go help
go help get
go env
go env GOPATH
go doc filepath.Glob
Install go
https://go.dev/doc/toolchain
/usr/local/go # installation dir
/usr/local/go/bin/go # binary location
Install go
wget -c https://go.dev/dl/go1.25.6.linux-amd64.tar.gz
sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go1.25.6.linux-amd64.tar.gz
go version
Change GOPATH
# in ~/.bashrc
export GOPATH=$HOME/bin/go
export PATH=$PATH:$GOPATH/bin
Update go (change version)
# update to go1.25.6
go env -w GOTOOLCHAIN=go1.25.6+auto
Uninstall go
sudo rm -fr /usr/local/go
# then remove go from $PATH
gopls
sudo apt remove gopls
go install golang.org/x/tools/gopls@latest
which gopls
gopls version
goimports
go install golang.org/x/tools/cmd/goimports@latest
goimports -w file.go
Start / run a program
go mod init myproject/example
go mod tidy
go help mod
go mod <command> [arguments]
The commands are:
download download modules to local cache
edit edit go.mod from tools or scripts
graph print module requirement graph
init initialize new module in current directory
tidy add missing and remove unused modules
vendor make vendored copy of dependencies
verify verify dependencies have expected content
why explain why packages or modules are needed
vi main.go
package main
import "fmt"
func main() {
fmt.Println("Hello, world.")
}
# ?????
go install example/user/hello
# compile and run
go run main.go
# build en executable and execute it
go build main.go && ./main
gofmt
go doc cmd/gofmt
# recursively gofmt files overwrite and print names of overwritten files
gofmt -l -w .
gofmt -w file.go
gofmt <filename or directory>
gofmt -w file.go # overwrite file.go
gofmt -d file.go # print the diff
gofmt -l *.go # list the file names that differ
-comments=true # Print comments; if false, all comments are elided from the output.
-tabs=true # Indent with tabs; if false, spaces are used instead.
-tabwidth=8 # Tab width in spaces.
gofmt [flags] [path ...]
The flags are:
-d
Do not print reformatted sources to standard output.
If a file's formatting is different than gofmt's, print diffs
to standard output.
-e
Print all (including spurious) errors.
-l
Do not print reformatted sources to standard output.
If a file's formatting is different from gofmt's, print its name
to standard output.
-r rule
Apply the rewrite rule to the source before reformatting.
-s
Try to simplify code (after applying the rewrite rule, if any).
-w
Do not print reformatted sources to standard output.
If a file's formatting is different from gofmt's, overwrite it
with gofmt's version. If an error occurred during overwriting,
the original file is restored from an automatic backup.
air
Automatic reload on source change
curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
# cd /path/to/project/src
air init # creates .air.toml
air # runs the application using .air.toml
# in file .gitignore
src/tmp