Your first Go program

The first program of a language is a contract with its toolchain: write a file, build it, run it, read what it printed. In Go the file is short because the program starts at a function named main in a package named main, and everything else is imported from the standard library.

The program line by line

package main says that this file belongs to the executable package. import lists the packages the file uses: fmt for printing and os for the arguments and the exit status. Imports are written one per line in a parenthesised group, which is the form gofmt produces for two or more.

os.Args is a slice of strings: the program name first, then the arguments the shell passed. The slice expression os.Args[1:] drops the name. os.Exit(3) stops the process immediately with status 3, which is why it is the last thing this program can do.

package main import ( "fmt" "os" ) func main() { fmt.Println("Hello, Go") fmt.Println("program:", os.Args[0]) fmt.Println("arguments:", os.Args[1:]) if len(os.Args) > 1 && os.Args[1] == "fail" { fmt.Println("leaving with status 3") os.Exit(3) } }

go build -o hello hello.go
./hello
./hello one two
stat -c '%s bytes' hello

Hello, Go
program: ./hello
arguments: []
Hello, Go
program: ./hello
arguments: [one two]
2398470 bytes

Three lines of output, three different things: a literal, the path the shell used to run the binary and the arguments after it. The binary is the file the build wrote, and it can be copied to a machine without Go.

Arguments and the exit status

The shell reports the status of the last command in $?: zero for success, a non-zero number for a failure. A Go program that returns from main exits with zero; a program that calls os.Exit chooses its number.

package main import ( "fmt" "os" ) func main() { if len(os.Args) > 1 && os.Args[1] == "fail" { fmt.Println("leaving with status 3") os.Exit(3) } fmt.Println("everything is fine") }

go build -o args args.go
./args
./args fail; echo "status: $?"

everything is fine
leaving with status 3
status: 3

The status is part of the interface of a command line tool, and the tasks capstone gives every failure its own number. Inside a program the error value is the usual way to report a failure; the status is what the rest of the system sees.

A program that does not compile

The file below is missing its closing brace. The compiler reads to the end of the file and reports the position where the brace should have been, not the position of the statement that needed it.

package main import "fmt" func main() { fmt.Println("this file never compiles")

go build broken.go

# command-line-arguments
./broken.go:7:1: syntax error: unexpected EOF, expected }

Exercises

  1. Add the closing brace to broken.go and build it. Which other error, if any, does the compiler report next?
  2. Make the program print the number of arguments with len(os.Args[1:]), then run it with three arguments. What does it print?
  3. Run the program with the argument fail and print $? in the same command line, joined by ;. Which status does the shell report?

Questions

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

  1. What are the two names a Go executable needs?

    Answer

    package main and func main

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

  2. What is os.Args[0]?

    Answer

    The name the program was run with

    The slice starts with the program name, so the arguments are os.Args[1:].

  3. What does os.Exit(3) do?

    Answer

    It stops the process at once with status 3

    os.Exit ends the process immediately; deferred functions do not run.

  4. What status does a program have when main returns normally?

    Answer

    0

    Returning from main is a successful exit, and the shell sees zero.

  5. What does this program print? package main import ( "fmt" "os" ) func main() { fmt.Println(len(os.Args)) } when it is run as ./count one two

    Answer

    3

    os.Args holds the program name and both arguments, so its length is three.

  6. What does this program print? package main import ( "fmt" "os" ) func main() { fmt.Println(os.Args[1:]) } when it is run as ./show alpha beta

    Answer

    [alpha beta]

    Println formats a slice of strings with brackets and spaces.

  7. What does the compiler report first for this program? package main import "fmt" func main() { fmt.Println("this file never compiles")

    Answer

    unexpected EOF, expected }

    The parser reaches the end of the file before the closing brace of main.

  8. Which command writes a binary you can copy to another machine?

    Answer

    go build

    go run writes a temporary binary; go build keeps one in the current folder or under -o.

  9. What does $? hold in the shell after a program runs?

    Answer

    Its exit status

    The shell stores the status of the last command in $?, zero meaning success.

  10. Why does a Go file not end its statements with semicolons?

    Answer

    The compiler inserts them, so they are only needed to separate two statements on a line

    Go's grammar inserts a semicolon at a newline after a token that can end a statement.

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