My first Go mistakes & experiences

I really like Go. Every few months, I try to learn something new. When I started irrigation, I felt that it was the kind of project that could really benefits from using Go.

Learning Go has been a great experience. There were ups and downs and this post is mostly about the downs and how you can avoid them.


When I started, I didn't set $GOPATH environment variable. Don't do that. Always have a $GOPATH set when developing. Whether you use ZshBash or Windows Command Terminal, you only need to set the environment variable $GOPATH to a folder to have a working Go environment. This will let you leverage the go get package command and will also make sure that the compiler can find all the package your project requires.

Debugging: Printing variables content

Depending on where you are coming from, printing variable may or may not be obvious for you so let's cover this one really quickly.

package main import "fmt" func main() { var1 := someFunctionThatReturnsSomething # Print the variable fmt.Println(var1) # Print the variable with a format fmt.Printf("Variable1: %v", var1) }

Public vs Private

This one is well described in the documentation but from time to time I forget about it. In a given package, if you create a function with the first character of the function name is lowercase, the function won't be accessible outside the package. Public methods have an uppercase letter as their first character.

Private method

func myPrivateMethod() {}

Public method

func MyPublicMethod() {}

Package "main" vs everything else

You will find out rapidly that most if not all packages used in the learning process are name main. Main package is a special package that will be compiled as an executable. All the other package names you will build and install will be package object that can be used by other main package.

Obviously, there is always 1 main package in a given project. That being said, you can use more than 1 file in the same package.

Package can be a group of file

So you have a main package and it's now a few hundred lines of codes and you would like to group similar functions together for clarity. If you think your only way out is to create a sub-package, you might find this solution interesting:

You can have multiple file using package main. A great example is in irrigation, I am using gorilla/pat to handle my routes in a Ruby fashion. I didn't want to pollute irrigation because I wanted it to be mainly about processing flags and be easy to read.

I created a file handler.go where all my http functions are defined. Since both files are within the same package, both can access private variables & functions within main.

Sub-package exists

Sometimes, the solution above isn't enough. That's what sub-package is for. Let's start with how package works when you import them.

One example you probably stumbled upon is net/http package. Let me try to explain how this  works. Packages are independent of each other. Even if they are hierarchical, importing a child does not import the parent and vice-versa.

So, when you import net/http you only import http. The reasoning is simple: When you import a package, it is given a namespace that you can use. In our case, the default namespace is http (lowercase). For that reason it doesn't make sense that net would be imported as well. The global namespace would be polluted from a few imports.

So your project is named Foo and your sub-package is Bar. Let's create it:

$ cd Foo $ mkdir Bar $ touch bar.go

bar.go should have the following skeleton:

package bar func init() { ... } #inits work in sub-package too. It will be called BEFORE init in main

In your main package, you can now import "foo/bar" and it will include this package in your project. You can now call functions in bar from main.

However, you cannot import main in foo/bar.go. It would create a cyclic import and Go doesn't like it.

That's it!

I think I have covered most of the issues I had when starting Go.

If you liked this post, you should follow me on Twitter.

Get more ideas like this to your inbox

You will never receive spam, ever.