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

Why Go?

Why Go?

Go is becoming a popular language of choice for building apps and APIs. This talk will cover some of the top reasons why you might want consider Go:
- Simplicity
- Concurrency
- Performance

We will cover how Go differs from other languages and some of “the Go way” of doing things. Lastly we will discuss how we’ve used Go in production to build APIs in a micro service, gRPC world!

Mark St.Godard

June 04, 2019
Tweet

More Decks by Mark St.Godard

Other Decks in Programming

Transcript

  1. What is Go? • Created by Google (2007) ◦ Rob

    Pike, Ken Thompson, Rob Griesemer • Open Source (2009) • Statically typed • Compiles to an executable ◦ GOOS=linux GOARCH=amd64 • Garbage collected • Syntactically similar to C
  2. What is Go? • Technically not OO • Functions and

    Structs • Interfaces • Concurrency model ◦ Goroutines, channels • Pointers • Pass by value • First class functions
  3. What is NOT in Go? • No inheritance • No

    constructors or destructors • No overloading functions • No operator overloading • No generics (yet) • No exceptions
  4. Go interfaces • Interfaces are implicit • Types do not

    have to explicitly “implement” • Duck typing • Compile time
  5. Go interfaces • More than just a convenience • Loosely

    coupled • Favour thinner interfaces ◦ Compose interfaces • Define interfaces where they are used ◦ Next example... • Unit testability
  6. Go concurrency • Concurrency built into the language • Goroutines

    ◦ Function that can run concurrently with other funcs • Channels ◦ How goroutines can communicate and synchronize execution ◦ Can be buffered or unbuffered
  7. Go fmt • go fmt • Part of the go

    toolchain • Automatic code formatting ◦ Integrates with most editors, IDEs • Simplifies reading, writing code • Uncontroversial ◦ No more debates
  8. Go test • go test ◦ Unit testing is built

    into the language • Part of the standard library testing package • Basic but there are frameworks that extend • For info on how Bold does unit testing: ◦ https://medium.com/boldly-going/unit-testing-in-go-wit h-ginkgo-part-1-ce6ff06eb17f
  9. Go vet • go vet ◦ Static testing is built

    into the toolchain ◦ https://golang.org/cmd/vet/ • Linting • Unsafe or suspicious code • Great lint aggregators ◦ golangci-lint ◦ https://golangci.com/
  10. Things that may seem weird at first... • No inheritance

    or objects ◦ Composition over inheritance ◦ Embedded types but not subclassing • Go is not really a OO, but ◦ Types, methods and interfaces ◦ Encapsulation
  11. Things that may seem weird at first... • Order of

    variables and types ◦ variable < - > type
  12. Things that may seem weird at first... • Packages ◦

    Favours putting “like things” together in packages ◦ Organize around responsibility • Habits of splitting by layer ◦ models, services, controllers, ... • Does not allow cycles between packages
  13. Things that may seem weird at first... • Naming ◦

    Favours short names ◦ Packages, variables • The greater the distance between declaration and use, longer the name • Idiomatic Go-isms ◦ ok, err • What is in a name? (Andrew Gerrand)
  14. Things that may seem weird at first... • Multiple returns

    ◦ Go supports multiple return types ◦ Almost always 2 or less ◦ 2nd being of type error • Returning early ◦ Within a func, returning when an error ◦ No dogmatic “single return” • Example...
  15. Things that may seem weird at first... • Frameworks ◦

    Go standard library ◦ http package for HTTP handlers • There are some useful frameworks and libs ◦ gorilla: http routing, dispatching ◦ squirrel: fluent SQL building • Recommend looking at stdlib first
  16. +

  17. How Bold uses Go • Historically mostly PHP • Started

    adopting Go ~2016/2017 • PHP / Laravel ◦ Unnecessary overhead for a simple API • Workloads ◦ Background / async jobs ◦ REST APIs ◦ gRPC services
  18. How Bold uses Go • Microservices ◦ Decompose based on

    capability • Unit Tests ◦ ginkgo • Build ◦ Docker images • Deploy ◦ Kubernetes
  19. How Bold uses Go api gateway API 1 API 2

    API 3 API 4 API (n) https gRPC
  20. Kubernetes Node 1 Node 2 Node 3 API api gateway

    API api gateway API API API API API API API API API API API API
  21. Summary • Go is a great general purpose language •

    Simple, easy to learn • Concurrency model • Unit testing • Looking for frameworks? ◦ Rails (ruby), Laravel (php) • Lots of great resources online to learn...
  22. Resources • A Tour of Go ◦ https://tour.golang.org • Go

    by Example ◦ https://gobyexample.com • Go Playground ◦ https://play.golang.org • Boldly Going (our technical blog) ◦ https://medium.com/boldly-going