I am going to use a Windows 10 x64 Virtual Machine (VM) but Go is available for most popular platforms. I can already hear the infosec pros grunt. The Getting Started section on Go website has how-tos for most popular platforms. You can find binaries and building instructions.
You can get free Windows VMs from modern.ie. Make a snapshot after you everything is set up. They expire in 90 days and you can only re-arm them multiple times.
- Go to https://golang.org/doc/install and download the MSI binary.
- Install the MSI, choose the default location.
- Choose a development directory. I have created a shared directory in my VM. This way I can code in host and run the guest. In my case it 's
Z:\Go
where Z is the shared drive/directory. - Set the following environmental variables (installer might have already set some up):
GOROOT
:C:\Go
GOPATH
:Z:\Go
or the directory from step 3.
- Add
C:\Go\Bin
to PATH. - Open a new cmd and run
go env
. You should see what you have setup.
Output of go env
in my Windows 10 VM is:
$ go env
set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=Z:\Go\
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
-fdebug-prefix-map=C:\Users\IEUser\AppData\Local\Temp\go-build352203231=/tmp/go-build
-gno-record-gcc-switches
set CXX=g++
set CGO_ENABLED=1
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
You can write Go code anywhere but only code in a GOPATH
directory can be executed with go run
2.
Go to the development path in step 3 of last section and create three directories inside it:
src
: Source code.bin
: Compiled files.pkg
: Executables.
You can clone this repository in src
and then run everything in code
. The directory structure looks like in the Windows 10 VM:
Z:\Go>tree /F
Z:.
├───bin
├───pkg
└───src
└───Hacking-with-Go
└───code
└───01
01-01-HelloWorld.go
Let's write a quick "Hello World" application and run it.
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}
And we can run it with go run 01-01-HelloWorld.go
.
Z:\Go\src\hacking-with-go\code\01>go run 01-01-HelloWorld.go
Hello World!
Choose whatever you like. There are many editors with Go support (you will see below) to choose from. Some in no particular order are:
- SublimeText using GoSublime package.
- Atom via go-plus package.
- Visual Studio Code with Go extension.
- Vim-go.
- Emacs go-mode.
I personally use Sublime Text 3 and GoSublime.
The online go playground at https://play.golang.org/ is good for prototyping/testing and sharing quick scripts. It's pretty useful when Go is not installed on the machine. For more information read Inside the Go Playground.
It's possible to run both the playground and documentation server offline.
godoc -http :1234
will run the the documentation server atlocalhost:1234
.go tool tour
will start an offline version of Tour of Go atlocalhost:3999
. This allows coding offline in browser in Go playground.
gofmt
is Go's official formatting tool. It automatically modifies source code. The main reason behind choosing an editor with Go support is running gofmt
automatically on your code.
I personally do not agree with gofmt
. For example it uses tabs (I like spaces). Tab-width is fixed at four (I like two). But it's better if our code adheres to language standards.
For more information read go fmt your code. For usage see Command gofmt.
The starting curly brace needs to be on the same line as the the keyword starting the block (e.g. for
or if
). This is a Go standard enforced by the compiler. It's explained in the Go FAQ.
This is wrong:
func main()
{
fmt.Println("Hello World!")
}
This is correct:
func main() {
fmt.Println("Hello World!")
}