Install go
1.22.0 in /usr/local
curl -OL https://golang.org/dl/go1.22.0.linux-amd64.tar.gz
sudo tar -C /usr/local -xvf go1.22.0.linux-amd64.tar.gz
Run the following line or add them to .bashrc
export PATH=/usr/local/go/bin:$PATH
Initialize the go.mod
go mod init example.com/hello
Update dependencies
go mod tidy
To build your go code
go build
You can look up modules here https://pkg.go.dev.
-
Function with capital letter at the start is an exported function!
-
:=
is define and assignment 2 in 1 -
for local dependency, can do a simple hack fix like the following
go mod edit -replace example.com/greetings=../greetings
The command specifies that
example.com/greetings
should be replaced with../greetings
for the purpose of locating the dependency.The run this to synchronize the dependency
go mod tidy
-
for
is Go'swhile
-
Download external dependencies
go get golang.org/x/example/hello/reverse
-
Initialize workspace
go work init ./hello
-
Add additional module to the workspace
go work use ./example/hello
-
Name the test file with
_testing.go
-
All functions should start with
func Test..()
-
Run the test using
go test -v
Check the install path
go list -f '{{.Target}}'
Change bin location
go env -w GOBIN=/path/to/your/bin
Reset the bin location
go env -u GOBIN
Install the binary
go install
- Effective Go - https://go.dev/doc/effective_go
- Go module references - https://go.dev/ref/mod