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

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.

Josh Michielsen

September 21, 2019
Tweet

More Decks by Josh Michielsen

Other Decks in Programming

Transcript

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

    Nast (@condenasteng) → K8s, AWS, Go → Cyclist → Photographer → Dog Lover! @jmickey_ jmichielsen jmickey mickey.dev [email protected]
  2. 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
  3. 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
  4. 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.
  5. 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.
  6. Concurrency → “Goroutines” are lightweight threads that provide a simple

    concurrency primitive. → “Channels” allow for communication between Goroutines. → Goroutines are launched using the go keyword.
  7. 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
  8. 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.
  9. 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
  10. 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.
  11. 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
  12. → 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.
  13. 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