# Go Basics, With a Twist of Web Services : - Pre-talk reminders, for my own use: : - Mute notifications (Discord, Hangouts, Gmail, macOS, etc.) : - Reset the gofmt demo: `cp gofmt/main.go.bak gofmt/main.go` : - Charge iPad to use for notes via Sidecar : - Plug in your laptop : - Use the bathroom : - Shut your window Tags: go Summary: An introduction to using Go to build web services. Benjamin Carlsson Principal Software Engineer, SunPower ben@twos.dev twos.dev @glcls ## Handler basics .code -edit handler/main.go go run handler/main.go curl -i localhost:8080 : main() is the entrypoiont to your program. : Generally once it compiles, it works. ## gRPC Really Quickly .code grpc/service.proto ## Go is simple, reliable, and complete Go has first class support for: - Testing - Documentation - Dependency management - Building - Installing - Formatting - Benchmarking - Code generation : It takes all these things seriously, but addresses them all simply. : To contain all these things, we have... ## $ go help Usage: go [arguments] The commands are: build compile packages and dependencies fmt gofmt (reformat) package sources generate generate Go files by processing source get download and install packages and dependencies install compile and install packages and dependencies mod module maintenance run compile and run Go program test test packages ... : ./... ## Testing .code -edit testing/person.go /\/\//, .code -edit testing/person_test.go /\/\//, go test ./... : Testing is dead simple, you just call the function you want to test. ## go fmt vim gofmt/main.go : EXIT THE PRESENTATION AND RUN THIS COMMAND ## Exporting .play -edit exporting/person.go : aka public vs private : bring up zero-values here wrt admin ## go doc Run: godoc Open: .link http://localhost:6060/pkg/github.com/glacials/talks/go/web-basics/doc/ localhost:6060 Run: vim ~/go/src/github.com/glacials/talks/go/web-basics/doc/person.go ## Packages and directories /path/to/my/project/ golang.org/ x/ tools/ playground/ socket/ present/ // main.go import "golang.org/x/tools/playground/socket" : A package is a directory : Packages can have subpackages. : Importing a package imports all files in it. Files are arbitrary division lines to help YOU. : ~All packages can be imported by others, no bundling process. : In other languages you might extract shared libraries to a common space; in Go you'll : often import a package directly from an application repo. : e.g. this tool (`present`) itself imports golang.org/x/tools/playground/socket ## Return values .play -edit return/main.go .link https://pkg.go.dev/os?tab=doc#Open os.Open .link https://pkg.go.dev/io?tab=doc#Reader io.Reader : Go is a strongly, statically typed language. : Mention io.Copy, io.Reader, io.Writer WITHOUT saying their names, to lead into interfaces. : io.Reader/Writer work on S3, files, network streams, stdout, etc. ## type .play -edit type/main.go : Types come after the variable name in declarations. : Untyped string constant ## struct .play -edit struct/main.go ## Interfaces - Interface is a type - Interfaces are implicit .play -edit interfaces/main.go /main/, ## Interface patterns .play -edit interface/main.go /\/\//, .play -edit interface/user/user.go /\/\//, : Run main.go, not user.go :P : An interface is a type, just like string or int or struct. : : Stringer : Accept interfaces, return structs. : Why? : 1. Godocs are easier : 2. Your struct can accidentally implement interfaces : 3. It's an unneeded abstraction : This is uniquely possible with implicit interfaces. ## Context .play -edit context/main.go : This program errors, and we can have some context as to why : Mention that you can also use contexts for timeouts ## Goroutines .play -edit goroutine/main.go : Goroutines are cheap. : You can have hundreds of thousands on one box with no noticeable performance decrease. : Show that this program blocks by default, then write `go ` in front of `sendEmail()`. : Mention things built on top of them: : http.Handler, : sync ## Other resources .link https://golang.org/doc golang.org/doc .link https://tour.golang.org/list A Tour of Go .link https://golang.org/doc/effective_go.html Effective Go - Googling for "<thing> go" not working? Try "<thing> golang". : - map[string]struct{} : - Generics : - iota