Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Go

Introduction to Go

Presented in Portuguese (Brazil) here: https://www.youtube.com/watch?v=VKOxJVS-mvA&feature=youtu.be

Gabriel Sobrinho

May 28, 2020
Tweet

More Decks by Gabriel Sobrinho

Other Decks in Programming

Transcript

  1. Introduction • Blazing fast • Compiled • Strongly statically typed

    (with inference) • No exception handling • Concurrency using lightweight threads
  2. Exceptions • Use multiple return values to emulate try/catch •

    Use defer to emulate try/catch/finally • Defer stacks functions to be called when the closest function execution is done
  3. func main() { data, err := ioutil.ReadFile("/tmp/file") if err !=

    nil { fmt.Println(err.Error()) return } fmt.Println(data) }
  4. func main() { var file, err = os.OpenFile("/tmp/file", os.O_RDWR, 0644)

    if err != nil { fmt.Println(err.Error()) return } defer file.Close() var len, err = file.Write([]byte("my content")) if err != nil { fmt.Println(err.Error()) } }
  5. Coroutines • Lightweight threads • Used for async processing •

    Any function can be called in a coroutine • Communication using channels • You can wait for the result using select
  6. func main() { tc := make(chan bool) rc := make(chan

    string) go timeout(tc) go read(rc) select { case word := <-rc: fmt.Println("Received", word) case <-rc: fmt.Println("Timeout.") } }
  7. func read(ch chan string) { fmt.Println("Type a word, then hit

    Enter.") var word string fmt.Scanf("%s", &word) ch <- word } func timeout(ch chan bool) { time.Sleep(5 * time.Second) ch <- true }
  8. func main() { tc := make(chan bool) rc := make(chan

    string) go timeout(tc) go read(rc) select { case word := <-rc: fmt.Println("Received", word) case <-tc: fmt.Println("Timeout.") } }
  9. $ go run concurrency.go Type a word, then hit Enter.

    Timeout. $ go run concurrency.go Type a word, then hit Enter. hello<CR> Received hello
  10. There is also sync.WaitGroup if you need to wait for

    a specific number of coroutines to finish Another grossly comparison to JavaScript's Promise.all()
  11. What have you done so far with Go? • A

    service that invokes a text-speech engine in macOS • A service that profile and search fingerprints using a DLL in Windows • A service that exposes local files in both platforms for reading/writing • All exposed through REST APIs in localhost
  12. What the community have done so far with Go? •

    docker - a set of tools for deploying Linux containers • kubernetes - container management system • openshift - RedHat's PaaS • terraforma - infrastructure provisioning tool • caddy - open source HTTP/2 web server with HTTPS capability • mattermost - a teamchat system
  13. Who is notable using it? • Cloudflare • Dropbox •

    Google • Netflix • SoundCloud • Uber • And more...