1. Getting Started
Installation
Install it with homebrew, add it to the PATH and check the version to make sure it installed correctly. The command below will work for Macs with an M1, M2, M3, etc. chip.
arch -x86_64 brew install go
export GOPATH=$HOME/go
export GOROOT="$(brew --prefix golang)/libexec"
export PATH="$PATH:${GOPATH}/bin:${GOROOT}/bin"
go version
The go
command
go mod init
Run the go mod init
command, giving it your module path -- here, use example.com/greetings
. If you publish a module, this must be a path from which your module can be downloaded by Go tools. That would be your code's repository.
$ go mod init example.com/greetings
go: creating new go.mod: module example.com/greetings
go run
We can run a simple code block like:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
with the command go run hello.go
where hello.go
is the name of the file. This will print “Hello, world!” into the terminal. There is no binary file saved in the folder because go run
will build the binary, execute it and deletes it. This is useful for quickly testing smaller programs.
go build
To actually build the binary, we run go build hello.go
which creates an exectuable called hello
(or hello.exe
on windows). It will match the name of the file or package that was passed in.
go fmt
Format your code to remove all whitespace, extra indentations, etc.
go vet
We can fix errors such as passing the wrong number of parameters to formatting methods or assigning values to unused variables. go vet
will find these kinds of errors.
func main() {
fmt.Println("Hello, World!")
var hello int = 1;
}
nick@Nicks-MacBook-Pro-2 ch1 % go vet
# ch1/ch1
vet: ./hello.go:7:6: hello declared and not used
Third-Party Go Tools
Instead of using something like Maven (Java) or NPM (JavaScript), we can share the projects via source code repositories. E.g.
go install github.com/rakyll/hey@latest
The @
is the version we want to install. Once this installs hey
, we can use it as it’s installed into our $GOPATH/bin
directory - we can run it with:
hey https://www.nickderaj.com
golint
We want to enforce idiomatic Go code across our files, so we use tools like linters to make this happen automatically. One of the tools we can use is called golint
which we install with go install [golang.org/x/lint/golint@latest](http://golang.org/x/lint/golint@latest)
and simply run the golint
tool. It suggests:
- Properly naming variables
- Formatting error messages
- Placing comments on public methods and types
- etc.
golangci-lint
Combines multiple tools like golint
, go vet
and multiple other code quality tools into one. It can be installed with the following
# binary will be $(go env GOPATH)/bin/golangci-lint
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.55.2
golangci-lint --version
then simply run golangci-lint run
to check the code for any errors
Makefile
We can create a Makefile that contains the following:
.DEFAULT_GOAL := build
fmt:
go fmt ./...
.PHONY:fmt
vet:
go vet ./...
.PHONY:vet
lint:
golint ./..
.PHONY:lint
build:
go build hello.go
.PHONY: build
This will run all of the commands to make sure the code is formatted, vetted for non-obvious errors and compiled.