GoLang notes
- Documentation
- GoLang Installation
- Embed
- Functions and return types
- Golang - Working with objects
- Pointers
- Arrays and Slices
- Maps
- Concurrency
- GoLang Testing
- Tutorials
- GoLang Modules
- GoLang Web frameworks
- GoLang - Working with databases
- GoLang - Formatting and Styling
- What if the files get too big?
- Tooling
- Additional resouces and ideas for projects
- TODO
Documentation
- https://go.dev/doc/
- The Go Tour is a great step-by-step introduction to Go fundamentals.
- A Tour of Go: https://go.dev/tour
- FAQ: https://go.dev/doc/faq
- Purpose of Go: https://go.dev/doc/faq#What_is_the_purpose_of_the_project
- Go Blog: https://go.dev/blog/
- Go Playground: https://go.dev/play/
- If you’re new to Go, you’ll find useful best practices described in Effective Go and How to write Go code.
- Effective Go: https://go.dev/doc/effective_go
- How to write Go code: https://go.dev/doc/code
- Guiding principles in the design: https://go.dev/doc/faq#principles
- User manual: https://go.dev/doc/
- The Go Programming Language Specification: https://go.dev/ref/spec
- Standard library: https://pkg.go.dev/std
- String literals: https://go.dev/ref/spec#String_literals
- Learning: https://go.dev/learn/
- Go by Example: https://gobyexample.com/
- 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
Golang - Working with objects
Pointers
Arrays and Slices
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
Concurrency
https://go.dev/tour/concurrency/11
GoLang Testing
Tutorials
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.
- https://stackoverflow.com/questions/12913118/whats-the-benefit-of-defining-go-methods-away-from-struct-definitions
- https://www.reddit.com/r/golang/comments/188rq95/question_about_rest_api_structure_i_have_big/
Tooling
- VSCode extensions:
- Debuging - Step through go code
- https://code.visualstudio.com/docs/languages/go
- https://github.com/golang/vscode-go/wiki/debugging
- https://github.com/go-delve/delve
- In the debugger in Visual Studio Code, select main.go and click on
Run debugger
Additional resouces and ideas for projects
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
- https://blog.devgenius.io/listening-to-multiple-channels-in-go-11a1c6cd3a21
- https://medium.com/@jpoly1219/watch-out-for-these-tricky-things-in-go-a1109f68549c
- https://blog.devgenius.io/the-hidden-costs-of-ai-coding-assistants-insights-from-a-senior-developer-76274fe6b345
- https://blog.devgenius.io/trees-in-go-9b6ff346dcfc
- https://cs.opensource.google/go/x/tour/+/refs/tags/v0.1.0:tree/tree.go
- https://www.permit.io/blog/role-based-access-control-rbac-authorization-in-golang