Why you should read this
Ok, I know we have thousands of different (and better) articles explaining how to start coding in Golang (tour of Go, Effective Go and etc). Considering it, I’m going to focus on real examples (and some code) to make your Go journey a little bit easier, i.e., how to organize your go code, how to connect to a database, how to log things and what framework options (if any) to consider.
Why you should use Go as your first-class programming language
I’m not going to spend time on this topic too because we already have a lot of blog posts, talks and real life examples explaining why you should use Go programming language.
If you are still uncertain about the future fo Go, you should give a chance to these following links:
Golang success stories
Migrate from Scala to Go
Why Golang is the future
Go advantages over java and python
Why you should learn Go
Docker and Go, why we decide to write docker in Go
Also, you have to take into account that Google, Amazon, Datadog, HashiCorp and a lot of other companies are using it to build reliable server side software and infrastructure.
How to start coding
As I said, we already have a lot of excellent articles explaining how to set-up your IDE, download Go framework and etc.
We have two different types of content here:
Infrastructure and syntax: (Installing Go, IDE, language syntax and etc)
For this type I recommend starting with Golang Wiki, specially the “getting started” topic, then move to Dave Cheney’s blog https://dave.cheney.net/resources-for-new-go-programmers
How to write idiomatic Go (“Write Go as a gopher”)
This is the most important topic for newcomers. Usually, programmers from differents languages tend to reproduce structure and behavior that they are used to. However, Go has a very strict and specific (opinionated) way of doing things. I recommend taking a look at these links:
1) Effective Go
2) Code Review Comments
3) Idiomatic Go - Dmitri
4) Idiomatic Go - Gryski
5) Practical Go - Cheney
6) dotGo Talk - Katrina Owen
Should I use a web framework?
I don’t think you need a web framework to write a webserver and/or a microservice. In fact, the Go community will “recommend” using the standard library as much as possible, however, I think it will be easier for you to start coding using a framework because you will have a guideline to follow. Frameworks have specific rules and patterns that help us avoid mistakes.
Of course, I’m a huge fan of the standard library and I like to write web servers using only the http.Server package. But, I believe sometimes we can cut some corners to sell, test or pivot an idea.
I want to use a framework. Which one should I pick?
If you really want to use a framework I suggest these (in order of preference)
1) Echo framework: echo is a very good lightweight, minimalist web framework with a rich toolset and a lot of plug-ins. You have a plug-in for almost every most common task (Basic Auth, JWT, CORS, Http/2, Session and etc)
2) Buffalo: I have been noticing more content about buffalo. It is far from a light framework since it has templating, orm, testing and a lot of accessories for the go development process. I think it could be a good option if you want to migrate from Java and, for unexplainable reasons, you like Spring.
3) Go kit: go-kit isn’t a web framework, it’s more like a microservices tool. Its main goal is to facilitate the building and connection of microservices. It has circuit breaker, remote calls (rpc) and etc.
How to organize your code
There’s no golden rule for how you should define your packages, however, some good practices may help you a lot:
1) Don’t over package: it’s better to have fewer larger packages than small meaningless ones. In Go you don’t need a new package for everything. In a JVM world, people tend to create a new maven/gradle module for every new integration, database, messaging system or any other random tasks.
There’s a famous quote by Cheney about that:
“Coming from Java?
If you’re coming from a Java or C# background, consider this rule of thumb. - A Java package is equivalent to a single .go source file. - A Go package is equivalent to a whole Maven module or .NET assembly.”
And remember, in Go, the fewer the better.
2) Use simple and short names: in Go you won’t see a packaged named like “entiy_api_gateway” because we don’t split using underscore. In fact, we don’t split at all, because we don’t have more than one word in the same package name. Package names will have short and concise name like “http” or “fmt”.
3) Pack by entity and not by layer: It’s very unusual to have a “controller” or a “commons” package in Go, because we don’t pack using layered architecture. Go packages are made of same entities. If you want to read more about entities, check out “Domain Driven Design” principles.
Talk is cheap, show me the code
I’ve made some examples to help and guide you through this migration process from somewhere else to a nice and beautiful Go code paradise.
Within the code repo you will be able to see how to connect to databases, log things, define a simple web server and so on.
You can see it here : Go Echo Bootstrap