Table of contents
  1. go run
  2. go build
  3. go install
  4. go test
  5. go get
  6. go fmt
  7. go vet
  8. go mod
  9. go doc
  10. go generate
  11. golint

go run

The go run command is used to compile and run a Go program in a single step. 123

package main
import "fmt"

func main() {
  fmt.Println("Hello, World!")
}

To run the program: go run myprogram.go.

go build

The go build command is used to compile a Go program into an executable binary.

To compile the program: go build myprogram.go.

go install

The go install command is used to compile a Go package and install it in the $GOPATH directory.

package main
import "fmt"
func main() {
  fmt.Println("Hello, World!")
}

To install the program: go install.

To run the installed program: myprogram.

go test

The go test command is used to run tests for a Go package.

package main
import "testing"
func TestMyProgram(t *testing.T) {
  result := MyProgram()
  if result != "Hello, World!" {
    t.Errorf("Expected 'Hello, World!', but got '%s'", result)
  }
}

To run the tests: go test.

go get

The go get command is used to download and install packages from remote repositories.

go get github.com/gin-gonic/gin

After the package is installed, you can import it into your Go code by adding the following line to your source code:

import "github.com/gin-gonic/gin"

go fmt

The go fmt command is used to format Go code according to the standard Go formatting rules.

go fmt myprogram.go

go vet

The go vet command is used to check Go code for common mistakes and errors.

go mod

The go mod command is used to manage dependencies in Go modules, which are a way of organizing and sharing code dependencies in Go.

Use the go mod init command to initialize a new Go module:

go mod init myproject

Next, you can use the go mod tidy command to download and manage the dependencies of your module:

go mod tidy

go doc

The go doc command is used to generate documentation for Go packages.

You can also use the go doc command to view documentation for specific functions or other elements within a package.

go doc mypackage.HelloWorld

go generate

The go generate command is used to run code generators in Go.

golint

The golint command is a tool for checking Go code for style and correctness issues.