Go (also known as Golang) is an open-source programming language developed by Google. It is a statically-typed compiled language. Go supports concurrent programming, i.e. it allows running multiple processes simultaneously. This is achieved using channels, goroutines, etc. Go has garbage collection which itself does the memory management and allows the deferred execution of functions.

Steps to follow

  1. Write the go program.
  2. Write the test case and benchmark code.
  3. Run the tests. Usually running  go test in file is all you need to do. If you can't get all the tests to pass, try to ask or fix it asap.
  4. Ensure your code is formatted with gofmt. Most editors that support Go can be configured to do this automatically. If you're coming to Go from other languages, you may not be used to the idea that there's one, and only one, accepted the way to format Go code, and it's the gofmt way. At first, this may seem overly restrictive, but there are great advantages to standard formatting, not least that it avoids a lot of futile arguments about which is the best way to format Go code
  5. Make sure your code passes golintgolint is a tool that analyses your code for common errors and problems, and also enforces things like documentation comments for your functions and identifiers. Again, your editor can usually lint your code automatically, and it's a good idea to set this up. Run go get -u golang.org/x/lint/golint to install, golint, and check your code with it.
  6. The goal of the exercise is to teach fluency. Among other things this includes guiding you to a simple, readable and idiomatic solution. While these are good software engineering principles in general, they're especially important in Go, where the whole language is designed for maximum simplicity and clarity.
  7. When you have something which works, try to simplify it as much as possible by eliminating all redundant or duplicated code, and rewriting everything in its simplest form. If there are parts of the code which seem awkward or complicated to you, trust your instincts and refactor the code until you feel good about it. In Go, 'clear is better than clever'. Keep that in mind and you won't go far wrong.
  8. Resist the temptation to optimize everything for performance. Go programs are fast; astonishingly fast, if you're used to interpreted languages. Go also has great performance analysis tools: the benchmarker, the profiler, and so on. These are all great fun to play with, and as engineers, we love trying to find the absolutely optimal way to do something. Feel free to do so but always consider simplicity and readability first. If you find a more efficient method which doesn't compromise readability, that is perfect!
  9. Code formatting in Go is very important to the community. Take a look at gofmt and run it on your code. Your editor or IDE should be able to do this for you automatically:
  10. DRY isn't always better. It may sometimes be better to duplicate small things than to introduce unnecessary complexity just to avoid duplication.
  11. Go has great guidelines about how to write comments. There's a standard format you can use for documentation comments, which can then be turned into beautiful documentation automatically by the godoc tool.

Formatting code with gofmt

If you are using an IDE, then you can use the go extension mentioned before.

If you want to get to know gofmt you can run it manually:

# will only show the differences
gofmt -d filename.go

# will apply the changes
gofmt -w filename.go

Writing comment

Go has great guidelines about how to write comments. There's a standard format you can use for documentation comments, which can then be turned into beautiful documentation automatically by the godoc tool.

The package comment should start with the word Packagefollowed by the package name, like this:

// Package cook provides handy conversion methods for units typically used in recipes.
package cook

For exported functions, methods, constants, and package variables, the comment should start with the name, and continue with a verb phrase:

// TbsToMl converts tablespoons to milliliters.
func TbsToMl(tbs int) int {
	// ...

In addition to optimizing for readability, following the recommendations improve how your code looks in Go's documentation tools such as godoc.orggodoc, and go doc.

Using golint

There's a great tool called golint which will examine your code for common problems and style issues. Try running golint on your code, e.g.: golint filename.go; it will make some useful suggestions for you.

It's a good idea to always check your code with golint (or configure your editor to do it for you). When you're writing Go software for production, many build pipelines will automatically fail if the code doesn't pass golint (and gofmt). You can avoid this by linting code yourself prior to submitting it.

If you are keen on getting much more feedback from code linters there is the golangci-lint project allowing you to run a lot of linters with one command. You can create a config file for your project to adjust the behavior to your needs.

Running the tests

To run the tests run the command go test from within the src directory.

If the test suite contains benchmarks, you can run these with the --bench and --benchmem flags:

go test -v --bench . --benchmem