This blog post will list some of the basic things you should really do before committing Go code into your repository.
1) Run gofmt/goimports
gofmt is probably the most popular Go tool amongst gophers. The job of gofmt is to
format Go packages, your code will be formatted to be consistent across your code base. For example if we have the following if statement:
ifx==42 { fmt.Println("The answer to everything") }
Then running gofmt on this will format the code to:
On Friday the 21st of August I attended the Golang UK conference 2015 held at
the amazing Brewery in London. This post is a short write up of my time at the
conference.
This was my first ever conference so apart from the talks, I did not know what
else to expect. Overall though, I found the conference was excellent and I met
a wide range of interesting people.
One thing I absolutely love about Go is its tooling support. Whenever I use the
numerous tools I always discover something new. In this short post I will be
showing off gofmt’s -r flag, this flag allows you to apply a rewrite rule to your
source before formatting.
A rewrite rule is a string in the following format:
pattern -> replacement
Both pattern and replacement must be valid Go expressions (more on this later), lets
apply a simple rewrite to the following code:
When writing Go code you should try to stay away from concatenating strings using the ‘+’ and ‘+=’’ operators.
Strings in Go, like many other languages (Java, C#, etc…) are immutable, this means after a string has been created it is impossible to change. Here is what the Go Programming Language Specification has to say about the string type:
“A string type represents the set of string values. A string value is a (possibly empty) sequence of bytes. Strings are immutable: once created, it is impossible to change the contents of a string. The predeclared string type is string.”