GoLang notes

Documentation

  1. https://go.dev/doc/
  2. The Go Tour is a great step-by-step introduction to Go fundamentals.
    1. A Tour of Go: https://go.dev/tour
  3. FAQ: https://go.dev/doc/faq
  4. Purpose of Go: https://go.dev/doc/faq#What_is_the_purpose_of_the_project
  5. Go Blog: https://go.dev/blog/
  6. Go Playground: https://go.dev/play/
  7. If you’re new to Go, you’ll find useful best practices described in Effective Go and How to write Go code.
    1. Effective Go: https://go.dev/doc/effective_go
    2. How to write Go code: https://go.dev/doc/code
  8. Guiding principles in the design: https://go.dev/doc/faq#principles
  9. User manual: https://go.dev/doc/
  10. The Go Programming Language Specification: https://go.dev/ref/spec
  11. Standard library: https://pkg.go.dev/std
    1. fmt: https://pkg.go.dev/fmt
  12. String literals: https://go.dev/ref/spec#String_literals
  13. Learning: https://go.dev/learn/
  14. Go by Example: https://gobyexample.com/
  15. Managing dependencies: https://go.dev/doc/modules/managing-dependencies

GoLang Installation

Embed

https://gobyexample.com/embed-directive

Think of it as reading files from src/main/resources at application runtime. When building containers, the files have to be embedded in order for them to make it into the container. If not, we will see file not found exceptions at runtime.

Functions and return types

  1. https://go.dev/tour/basics/4

Golang - Working with objects

Pointers

  1. https://go.dev/tour/moretypes/1
  2. https://go.dev/tour/moretypes/4

Arrays and Slices

  1. https://go.dev/tour/moretypes/6
  2. https://go.dev/tour/moretypes/8
  3. https://go.dev/tour/moretypes/13

Testing if two slices are equal:

https://stackoverflow.com/questions/15311969/checking-the-equality-of-two-slices

For primitive types, we can use this:

func areSlicesEqual(a, b []int) bool {
        if len(a) != len(b) {
                return false
        }
        for i := range a {
                if a[i] != b[i] {
                        return false
                }
        }
        return true
}

Maps

https://stackoverflow.com/questions/32867780/filtering-a-slice-of-structs-based-on-a-different-slice-in-golang

Concurrency

https://go.dev/tour/concurrency/11

GoLang Testing

Tutorials

  1. https://go.dev/doc/tutorial/

GoLang Modules

GoLang Web frameworks

GoLang - Working with databases

GoLang - Formatting and Styling

What if the files get too big?

You can always break up a struct’s methods over multiple files in the same package too which may help with digestibility.

The type you define methods on must reside in the same package.

What you can do is to define functions and methods wherever you like inside the package. It doesn’t really matter if the type definition is in the same file as the method definition for the type.

This makes it possible to group all type definitions in one file and have the method implementation in another. Possibly with other helper which are needed by the methods.

  1. https://stackoverflow.com/questions/12913118/whats-the-benefit-of-defining-go-methods-away-from-struct-definitions
  2. https://www.reddit.com/r/golang/comments/188rq95/question_about_rest_api_structure_i_have_big/

Tooling

  1. VSCode extensions:
    1. https://github.com/golang/vscode-go
  2. Debuging - Step through go code
    1. https://code.visualstudio.com/docs/languages/go
    2. https://github.com/golang/vscode-go/wiki/debugging
    3. https://github.com/go-delve/delve
    4. In the debugger in Visual Studio Code, select main.go and click on Run debugger

Additional resouces and ideas for projects

https://gophercises.com/

Go has the best standard http library. Do a little middleware chaining for logs and errors and that will give you a solid grasp of lots of patterns without being too complicated.

build something like small web servers with net/http & then go for a frameworks like gin or fiber with database integrations & caching systems like redis. build small cli tools for yourself. try to write some small program for your daily repetitive tasks.

My first Go project was replacing a bash script that was too slow. I learn about Go routine and then I DoS my service by having Go be far too efficient and had to learn about waitgroups and channels.

So, obviously getting familiar with the core language is best but here’s some really handy libraries to keep in mind.

CLI flags parsing:

Config Loading:

Or just search for awesome-go and find a more choices than you know what to do with. https://github.com/avelino/awesome-go

Check out this great blog post by the Go team for more details on the design and implementation of slices in Go: https://go.dev/blog/slices-intro

TODO

  1. https://blog.devgenius.io/listening-to-multiple-channels-in-go-11a1c6cd3a21
  2. https://medium.com/@jpoly1219/watch-out-for-these-tricky-things-in-go-a1109f68549c
  3. https://blog.devgenius.io/the-hidden-costs-of-ai-coding-assistants-insights-from-a-senior-developer-76274fe6b345
  4. https://blog.devgenius.io/trees-in-go-9b6ff346dcfc
  5. https://cs.opensource.google/go/x/tour/+/refs/tags/v0.1.0:tree/tree.go
  6. https://www.permit.io/blog/role-based-access-control-rbac-authorization-in-golang

Links to this note