What Go is and where it runs

Go is a compiled, statically typed language with a small grammar, a garbage collector and one build tool. It was designed at Google for programs that are read by many people for many years: servers, command line tools and the plumbing between them. A Go source file declares a package, imports the packages it uses and defines functions, and the whole language fits in a short specification.

This lesson is the tour before the work: a first program, the targets the compiler can build for, the commands that drive a project, and the two errors a beginner meets first. Every program below was compiled with the Go toolchain named at the end of the page and run here, and the output is the output it printed.

Hello, Go

A Go program is a package named main with a function named main. Inside it, fmt.Println writes a line to standard output: fmt is the formatting package of the standard library, and Println is one of its functions. There is no semicolon at the end of the line: the compiler inserts them, which is why an opening brace stays on the line of its statement.

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

go build -o hello hello.go
./hello
file hello
stat -c %s hello

Hello, Go
hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, Go BuildID=9XPfNbTE7y2Qn6_fq3Go/y2ZfP6WGWqHA-0TYhasO/9eZZHtItQb6eJmLdRy7F/ga3SsmonttbV0U_TlhVx, BuildID[sha1]=08bf0fa805ca696b20b0816acdc72cc7c6c9960e, with debug_info, not stripped
2397326

The file line describes the result of the build: a 64-bit executable for x86-64, statically linked, with the Go build id in it. The binary does not need the source file and does not need Go installed, and it needs no shared library of this Alpine system either. The last line is its size in bytes: 2.4 megabytes, because it carries the runtime and the parts of the standard library that it uses. That size is the price of a deployable file.

Where Go runs

The runtime package reports the constants of the build, not of the machine: GOOS and GOARCH are fixed when the program is compiled, so a binary built for Linux on ARM prints linux and arm64 even when you read the source on an x86 laptop. The pointer width is computed from uintptr, so the same expression is true on every target.

The compiler knows a long list of targets, and the count is a property of the compiler version, not of the language: it grows when a port is added. The program prints its own build constants, and the last command prints the environment the build reads.

package main import ( "fmt" "runtime" ) func main() { fmt.Println("go version:", runtime.Version()) fmt.Printf("os: %s\n", runtime.GOOS) fmt.Printf("arch: %s\n", runtime.GOARCH) fmt.Printf("compiler: %s\n", runtime.Compiler) fmt.Printf("pointers: %d bits\n", 32<<(^uintptr(0)>>63)) }

go tool dist list | wc -l
go run where.go
go env GOOS GOARCH GOVERSION

47
go version: go1.26.10
os: linux
arch: amd64
compiler: gc
pointers: 64 bits
linux
amd64
go1.26.10

A binary built for one target does not run on another. Cross-compiling is a matter of setting two environment variables before the build, and the lesson on cross-compiling does that for a real program. The install pages of this course cover Ubuntu, Rocky Linux, macOS, Windows 11, FreeBSD and Docker.

The go command and gofmt

The go command is the whole toolchain: it compiles, links, tests, formats, documents and downloads. Four subcommands cover most of a working day. go run compiles a program into a temporary file and runs it, which is what you want while writing. go build writes the binary where you can keep it. go test compiles the test files of a package and runs them. go doc prints the documentation of a package or a symbol from the source itself.

gofmt is not a suggestion: the formatter is part of the toolchain, every editor runs it on save, and gofmt -l prints the files that are not formatted. Below, the program is built and run, a deliberately messy file is written next to it, and gofmt is asked which file it would change and what it would change. go vet then reads the program for mistakes the compiler accepts.

package main import "fmt" func main() { fmt.Println("built with the go command") }

go run format.go
printf 'package main\nfunc main(){println("hi")}\n' > messy.go
gofmt -l .
gofmt -d messy.go
rm messy.go
go vet format.go

built with the go command
messy.go
diff messy.go.orig messy.go
--- messy.go.orig
+++ messy.go
@@ -1,2 +1,3 @@
package main
-func main(){println("hi")}
+
+func main() { println("hi") }

gofmt -l named messy.go and nothing else, and gofmt -d showed the patch it would apply: tabs for indentation, a space after a comma, a newline at the end of the file. go vet printed nothing, which is how a clean file answers. The next lesson wires both commands into an editor.

The two errors you will meet first

Go refuses a program that declares a variable and never reads it. The rule looks pedantic until you have read a file where half the declarations were left behind: the compiler keeps the file honest, and the fix is a deletion, not a silence.

package main import "fmt" func main() { name := "Go" fmt.Println("hello") }

go build unused.go

# command-line-arguments
./unused.go:6:2: declared and not used: name

The second error is an identifier that does not exist. The message names the position and the name, and nothing else: Go has no long explanation to offer, because the name is simply not in scope.

package main import "fmt" func main() { fmt.Println(greeting) }

go build undefined.go

# command-line-arguments
./undefined.go:6:14: undefined: greeting

Read the two messages together: one is about a value that is written and never read, the other about a name that is read and never written. The lesson on panics covers the third kind, the mistake that survives the compiler, and vet and linters cover the tools that catch it.

Exercises

  1. Change hello.go so it prints two lines, then run go build instead of go run and execute the binary. Where did the binary go, and what is its name?
  2. Print runtime.NumCPU() next to the constants. Does the number match the processor count of the machine you run it on?
  3. Compile the hello program with GOOS=windows go build. What does file say about the result, and can you run it here?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does a Go program start with?

    Answer

    A package named main and a function named main

    The go command builds package main into an executable, and the runtime calls its main function.

  2. What does go run hello.go do?

    Answer

    It compiles the program to a temporary binary and runs it

    go run is a build followed by an execution; the binary is temporary, and go build is the command that keeps one.

  3. Which command prints the files that are not formatted the Go way?

    Answer

    gofmt -l

    gofmt is the formatter; go fmt runs it over a package, while gofmt -l lists files without rewriting them.

  4. What does this program print? package main import ( "fmt" "runtime" ) func main() { fmt.Println(runtime.GOOS) }

    Answer

    linux

    GOOS is the operating system of the build target, and this program was built for Linux.

  5. What does the compiler say first about this program? package main import "fmt" func main() { name := "Go" fmt.Println("hello") }

    Answer

    declared and not used: name

    The variable name is declared and never used, so the build stops before the program prints anything.

  6. What does the compiler say first about this program? package main import "fmt" func main() { fmt.Println(greeting) }

    Answer

    undefined: greeting

    The name greeting was never declared, so the compiler reports it as undefined at its position.

  7. What does this program print? package main import "fmt" func main() { fmt.Println(32 << (^uintptr(0) >> 63)) }

    Answer

    64

    On a 64-bit target the shift produces 64, which is the pointer width in bits.

  8. Which rule makes this program fail to build? package main import ( "fmt" "os" ) func main() { fmt.Println("hi") }

    Answer

    "os" imported and not used

    An imported package that no code of the file uses is a compile error, and the fix is to remove the import.

  9. What name does go build give the binary of package main?

    Answer

    The name of the folder that holds the package

    go build names the binary after the folder that holds the package; the -o flag overrides it.

  10. What does go vet look at?

    Answer

    Suspicious code that compiles, such as a bad Printf verb

    vet is a set of analyzers for mistakes the compiler accepts, such as a format verb that does not match its argument.

The code on this page was run with go 1.26.10 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image