Rapid API Development in Go

Rapid API Development in Go

The Go programming language is a statically typed, compiled programming language designed at Google that was released in 2009. It grew quickly in popularity - holding a top 5 position in the most loved programming languages in the Stack Overflow developer survey for 5 consecutive years, and in 2018 was the fastest growing language on GitHub.

Much of Go's popularity can easily be attributed to it's clear and concise syntax, simple concurrency model, strong built-in tooling, and its ability to be easily compiled into a single statically linked binary. However, to me Go's greatest strength is its robust, "batteries included" standard library, which allows developers to rapidly build production ready applications with little to no external dependencies.

In this talk I will provide a brief overview of the Go programming language, and demonstrate how Go can be utilised to rapidly build production ready APIs with just the standard library - HTTP(S) server and all. Finally I will provide a brief demo of a simple API workflow, and talk about some small libraries and tools that can be used to improve the development process and general quality of life.

B3d9b66c0d46431017776efe58baa683?s=128

Josh Michielsen

September 21, 2019
Tweet

Transcript

  1. 2.

    About Me → Snr Software Engineer, Platform Engineering @ Condé

    Nast (@condenasteng) → K8s, AWS, Go → Cyclist → Photographer → Dog Lover! @jmickey_ jmichielsen jmickey mickey.dev j@mickey.dev
  2. 3.
  3. 4.

    Third Party Libraries A look at some popular 3rd party

    libraries that make API development easier. Conclusion What Makes Go Unique? Briefly explore what makes Go different from other languages. Language Introduction A quick dive into the basics of the Go Programming Language. Web & APIs in Go Jumping right into some code, I will demo how to utilise the Go standard library to create a simple web server and API. 01 AGENDA 02 03 05 04
  4. 6.

    Features → Clear and concise syntax → Simple concurrency model

    → Strong built-in tooling → Compiled into a single statically linked binary → Robust standard library → Faster than Python
  5. 9.

    package main import "fmt" func main() { friend := "DDD

    East Anglia" fmt.Printf("Hello, %s", friend) } Package declaration. Main pkg produces a binary. Import statements. Function declaration. Main pkg must have function main. Variable. Will infer the type based on the right side of :=. Print a string using the ‘fmt’ package.
  6. 10.

    Some Examples → Lets jump into VS Code and “go”

    though some examples. Links to these code samples and other are provided at the end of the slide deck.
  7. 11.

    Concurrency → “Goroutines” are lightweight threads that provide a simple

    concurrency primitive. → “Channels” allow for communication between Goroutines. → Goroutines are launched using the go keyword.
  8. 12.

    package main import ( "fmt" "time" ) func say(s string)

    { for i := 0; i < 5; i++ { time.Sleep(100 * time.Millisecond) fmt.Println(s) } } func main() { go say("world") say("hello") } world hello hello world world hello hello world world hello Via ‘A Tour of Go’ - https://tour.golang.org/concurrency/1 Goroutines
  9. 13.

    package main import "fmt" func main() { messages := make(chan

    string) go func() { messages <- "ping" } msg := <-messages fmt.Println(msg) } Channels Via ‘Go by Example’ - https://gobyexample.com/channels Make a channel. Channels are typed. The <- operator sends the string to the channel. Receive from the channel and assign to a variable.
  10. 14.

    Compilation → Go compiles code into a single binary. →

    Binaries can be statically linked. → Cross-platform compilation is built-in. → Supports various architectures. CGO_ENABLED=0 go build GOOS=windows go build GOARCH=arm go build http://bit.ly/go-build-values
  11. 16.

    Examples → Simple Web Server → JSON Response → Middleware

    → Dynamic Routing → Gorilla Mux 3rd Party Library Link to the repo with the examples is provided at on the final slide of the presentation.
  12. 18.

    Gorilla Toolkit → Toolkit of libraries that simplify web application

    and API development. https://www.gorillatoolkit.org/ HttpRouter → Performant & light HTTP router with QoL improvements over net/http. https://github.com/julienschmidt/httprouter Go Kit Micro → Another popular microservices “runtime”. https://github.com/micro/micro → Popular microservices framework. https://github.com/go-kit/kit
  13. 20.

    → Go is a fast, powerful language that provides a

    simple and concise syntax. → Web development and API development is possible with nothing but the standard library. → Cross-platform compilation allows you to run your applications almost anywhere. → Most 3rd party libraries simply add a nicer UX over the standard HTTP implementation.
  14. 21.

    VSCode Go Plugin → Great Visual Studio Code Go plugin.

    https://marketplace.visualstudio.com/items?itemNa me=ms-vscode.Go Go by Example → Fantastic Go language tutorial with annotated code samples. https://gobyexample.com/ Go in Action How to Write Go Code → Getting started guide by the language developers/designers. https://golang.org/doc/code.html → Highly coveted Go book. https://www.manning.com/books/go-in-action