Skip to content

Instantly share code, notes, and snippets.

@anxiousmodernman
Last active March 5, 2017 18:12
Show Gist options
  • Save anxiousmodernman/a3e9367f5f61cc6da790a557e849373a to your computer and use it in GitHub Desktop.
Save anxiousmodernman/a3e9367f5f61cc6da790a557e849373a to your computer and use it in GitHub Desktop.
Write Golang!

Write Golang!

Go makes writing robust, powerful software easy. Write it!

Prerequesites

  • set a GOPATH and all your code lives there (echo $GOPATH)
  • make sure Go is installed with go version
  • set an alias in your shell to make it easy to hack: alias golang="cd $GOPATH/src/github.com"

Get Other People's Code

You use go get to get code. It saves it under GOPATH along with your own code. Try it with my examples library:

go get -u github.com/anxiousmodernman/examples-go/...  

This fetches all packages under this repository. Now you can run something.

cd $GOPATH/src/github.com/anxiousmoderman/examples-go/ctrlc-executable
go build
./ctrlc-executable

NOTE: if you want to "go get" from private repositories, you have to edit your git config in the manner described here: https://gist.github.com/shurcooL/6927554

Read the docs

If you see a library imported, and you don't know how it works. Go to the docs immediately. For examples, the time package is used here. And since this is a built in package you can find it's docs in the standard library docs. Go is very readable, so don't be afraid to read libraries.

Set up your Editor

Go is easy to read for humans and easy to parse for computers. The tooling is really good. I recommend Microsoft's free VS Code editor. After that, you can open any directory from the command line with code .. Finally, make sure you install the Go extension. This will automatically download some command line tools.

Other stuff

  • Put $GOPATH/bin on your PATH. export PATH=$PATH:$GOPATH/bin This is needed because editors look for tools on your PATH to do cool stuff like auto-importing.
  • go build builds code in the current directory. go install builds stuff in the current directory, but puts the binary into $GOPATH/bin. Another good reason to have it on your PATH.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment