Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Go Dictionary
  3. go build / run / test

go build / run / test

Go provides a set of official commands commonly used during development. Use go build to compile, go run to execute directly, go test to run tests, go fmt to format code, and go vet for static analysis.

Syntax

// Compile and generate an executable.
// go build                   // Build the current package.
// go build -o myapp main.go  // Specify the output file name.
// go build ./...             // Build all packages.

// Run directly without building first (useful during development).
// go run main.go
// go run .

// Run tests.
// go test ./...         // Run all tests.
// go test -v ./...      // Run with verbose output.
// go test -run TestXxx  // Run only a specific test.
// go test -cover        // Measure test coverage.

// Format code (follows gofmt conventions).
// go fmt ./...

// Detect issues with static analysis.
// go vet ./...

Command Reference

CommandDescription
go buildCompiles a package and generates an executable.
go run [file]Runs a Go source file directly (builds a temporary binary and executes it).
go testRuns tests defined in files ending with _test.go.
go fmtFormats source code according to Go's standard style.
go vetDetects code patterns likely to cause bugs using static analysis.
go installBuilds a package and installs it to GOPATH/bin.
go doc [pkg]Displays documentation for a package.
go envDisplays Go environment variables.
go cleanRemoves build artifacts and cache files.
go versionDisplays the installed Go version.

Sample Code

// --- File: main.go ---

package main

import "fmt"

// Add returns the sum of two integers.
func Add(a, b int) int {
    return a + b
}

// Greet returns a greeting string.
func Greet(name string) string {
    return "Hello, " + name + "!"
}

func main() {
    fmt.Println(Add(3, 4))
    fmt.Println(Greet("Go"))
}

// --- File: main_test.go ---
// Test files must have names ending in _test.go.

/*
package main

import "testing"

// TestAdd tests the Add function.
func TestAdd(t *testing.T) {
    got := Add(3, 4)
    want := 7
    if got != want {
        t.Errorf("Add(3, 4) = %d, want %d", got, want)
    }
}

// TestGreet tests the Greet function.
func TestGreet(t *testing.T) {
    got := Greet("Go")
    want := "Hello, Go!"
    if got != want {
        t.Errorf("Greet(%q) = %q, want %q", "Go", got, want)
    }
}
*/

// --- Build and run examples ---

// Run directly with: go run main.go
// Output: 7
//         Hello, Go!

// Build with: go build -o myapp
// Run with: ./myapp

// Run tests with: go test -v
// --- PASS: TestAdd (0.00s)
// --- PASS: TestGreet (0.00s)
// PASS

// Check coverage with: go test -cover
// PASS    coverage: 100.0% of statements

Notes

Go test functions must start with Test and take *testing.T as their argument. Test files must have names ending in _test.go.

go fmt applies a consistent code style across the entire project. In Go, the standard format is used universally — there is no need to debate code style with teammates or AI tools.

go vet detects code that compiles but is likely to cause runtime bugs, such as mismatched printf format verbs. It is recommended to integrate it into your CI pipeline.

For module management, see Go Modules.

If you find any errors or copyright issues, please .