golangtutorialAll tutorials

Golang Hello World and Your First Go Program

Build a Golang Hello World program, understand package main and func main, accept an argument, then run and compile it with Go 1.24.7.

Fundamentals · Lesson 1Saved in this browser. No account required.

Write and run your first Go program, then extend it to accept a name from the command line. You will understand package main, func main, imports, arguments, formatted strings, go run, and the boundary between reusable logic and the executable entry point.

Works with Go 1.22 and later. The complete example was executed with Go 1.24.7 on Windows amd64.

A Go executable starts in package main

An executable Go program uses package main and defines a main function. The go command compiles the package, then the operating system starts the resulting program at that entry point.

The complete Go tutorial places this first program in the wider language path. If Go is not installed yet, follow download and install Go first.

Build a useful Hello World

This version keeps greeting behavior in a function. That small separation makes the decision testable without starting another process.

package main

import (
	"fmt"
	"os"
)

func greeting(args []string) string {
	if len(args) == 0 {
		return "Hello, Gopher!"
	}
	return fmt.Sprintf("Hello, %s!", args[0])
}

func main() {
	fmt.Println(greeting(os.Args[1:]))
}

Run it with a name:

go run main.go Gopher

Output observed with Go 1.24.7:

Hello, Gopher!

os.Args contains the executable name at index zero. Passing os.Args[1:] gives greeting only the user-supplied arguments. The empty-slice branch prevents an out-of-range panic when no name is supplied.

Understand every line

package main

Files in the same directory normally declare the same package. The special name main tells the toolchain this package builds an executable rather than an importable library.

Imports

fmt supplies printing and formatted strings. os exposes process information, including command-line arguments. Go rejects unused imports, which keeps dependencies visible and deliberate.

func main()

The runtime calls main after package initialization. It takes no arguments and returns no value. Use return values and errors in ordinary functions, then let main decide what to print or which exit code to use.

Turn the first program into a small command

Extend the program in three steps:

  1. Accept --shout and uppercase the greeting.
  2. Move option parsing into a function returning a value and an error.
  3. Add table-driven tests for no name, one name, and invalid arguments.

When the command grows beyond one file, read Golang file structure before creating packages. When you want an executable file rather than a temporary run, use Go compilation and execution.

Common mistakes

Running from the wrong directory

If the terminal cannot find main.go, change into the directory containing the file or run the package path explicitly.

Naming the package something other than main

A package intended to build a command needs package main and a main function. Importable packages should use descriptive package names and must not contain the command entry point.

Indexing arguments without checking length

This unsafe assumption panics when the command receives no name:

name := os.Args[1] // broken when len(os.Args) < 2

Check the length or pass os.Args[1:] into a function that handles empty input.

What next

Learn the difference between go run, go build, and go install in Go compilation and execution. Then follow the beginner course and use the study guide to turn this command into a tested project.