Go programming language comes with a set of tools along with the standard installation.
gofmt
gofmt main.go: Prints the formatted source code of main.go file on the console.
gofmt -w main.go: Writes the formatted code in the file main.go
gofmt -w Tasks: Runs gofmt on all the files in the folder Tasks.
go fmt can be used in place of gofmt -w.
It is highly recommended to rungofmtbefore committing to version control.
godoc
extracts documentation comments on all the Go projects present in$GOPATH/src, and the standardlibrary present in$GOROOT
web interface
godoc -http=:6060
godoc also allowsusers to read the Go source code of the packages. Since Go is now implemented in Go itself,we can read the source code of Go language.
Using the -v flag, we come to know when the server gotupCommandline
godoc net/http
This will provide the documentation of net/http on the terminal, like the man command. Thecatch here is that we need to know the exact library name
go test
Go has testing support built into the language.
For each code filefile.go, the corresponding testcases should be present in a file named asfile_test.goin the same folder. The Go compiler ignoresall the*_test.gofiles while building the application.
go build
We can build our application usinggo build. It parses all the.gofiles except the*_test.gofilesin the entire folder and all sub folders along with imported libraries if any, and creates a staticallylinked binary. The binary name is the same as the project folder name, if we want a custom namewe should use the -o flag.
Example:go build -o tasks
go buildbuilds the entire application and the depending libraries, into a static binary andlater throws all away. This results in rebuilding everything every single timego buildis executed.For caching the library builds, usego installfirst andgo buildlater
cross compilation
Go allows cross compilation. We have to pass the OS name as linux/darwin/windows as GOOS asshown in the below commands.
env GOOS=darwin GOARCH=386 go build -o tasks.app
env GOOS=windows GOARCH=386 go build -o tasks.exe
go install
Creates a statically linked binary and places it in$GOPATH/bin. Also creastes a.afile of the libraryand puts it in the $GOPATH/pkg folder. In future builds this library file will be reused until theunderlying code is changed
n Linux/Unix, this is done using:export PATH=$PATH:$GOPATH/bin. This line needs to be added toeither.bashrcor.profile, whichever is being used by the shell.The profile files are present in the home folder. Do a cd ~ and check for either of the files mentioned
go run
go run combines building and running the application in one command. t generates a binary in the temp folder and executes it. The binary file isn’t retained after the run
go get
This is the package manager in Go. It internally clones the version control repository parameterpassed to it, can be any local/remote git repository. It then runsgo installon the library, makingthe library available in$GOPATH/pkg. If the repository doesn’t have any buildable files then go getmight complain, but that happens after the repository is cloned
go clean
This command is for cleaning files that are generated by compilers, including the following files:
_obj/
_test/
_testmain.go
test.out
build.out
DIR(.exe)
DIR.test(.exe)
MAINFILE(.exe)
Other
go fix // upgrade code from an old version before go1 to a new version after go1
go version // get information about your version of Go
go env // view environment variables about Go
go list // list all installed packagesFor details about specific commands,go help .
Top comments (0)