This article is an expression of my thoughts and approach when learning Go recently. I found parts of Go to be a bit confusing (not the language itself, instead the tooling around it), so I documented the things that confused me in the Things I Encountered, Gotchas section. Hopefully that section helps other beginners that run into the same problems that I did. When I get a better idea of how it works I’ll add a updates.
Go has a lot of good learning resources, however it can be tricky to know where to start. I used these resources to get started (in this order, so starting with A Tour of Go)
A Tour of Go
Go has a lot of good resources to get started with, I started with the A Tour of Go, which provides a good intro of the language. It provides a good overview of the language, as well as some specifics (like how to do Concurrency). Also make sure to check out the Methods and Interfaces section, it takes you through defining methods on types.
I didn’t do this in one go, instead I did the basics and came back to it later.
This is a newer site created by Google to share resources about Go, it has a few handy features, including:
- Case studies from companies using Go, which is interesting to see where and why they are using Go.
- Tailored learning resources for specific application types, depending on if you want to make a CLI or a webapp, the guides provide resources to get started
- Package search, useful for finding libraries to use.
Go by Example
Once I had a good understanding of Go, I used this as a quick reference for doing common things as well as more complex things, including concurrency, JSON handling and testing.
The examples are really short and to the point, making it easy to understand the concept.
Things I Encountered, Gotchas
The concept of the GOPATH still doesn’t completely click with me. Its been explained similarly to how the PYTHONPATH works, but also where the source for your projects should be kept also. So it seems to be a mix of both libraries you download using
go get and projects your writing.
There is also a standard way of directory naming, with all the source code being under a top level
src directory, and projects being namespaced similar to a C# or Java project. For example
Older tutorials said that all your projects must be inside the GOPATH folder, however with the new Go feature called Go Modules, it says you should keep your modules outside of the GOPATH. It seems a bit confusing for me as a beginner.
One of the first things that suprised me was the lack of versioning when fetching depedencies with
I read a lot of tutorials and it seemed like the topic was never covered.
go get pulls the module from the source repo (usually a Github repo) and puts it in your GOPATH, however what happens if the developer of the module makes a breaking change and you refetch the project?
After doing some more reading I found that there was a lot of tools to handle versioning of dependencies, some from the official Golang project and some third party. Theres a whole wiki page dedicated to them.
From what I understand, the one to use now are Go Modules. There is another tool called
dep which seems to have a lot of effort put into it, the documentation is very comprehensive and its developed by the Go core team.
However it seems that
dep is deprecated in favour of Go Modules, the Go wiki page for package management tools says the following:
dep was the “official experiment.” The Go toolchain, as of 1.11, has (experimentally) adopted an approach that sharply diverges from dep. See Modules.
So it seems like Go Modules is equally as experimental as
Arraylist like structures
var names = String names = append(names, "Bob")