Go Basics

Hello World in Go: Structure of a Program

Write your first Go program and see what each part does: package main, import and func main.

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • package main and func main
  • Importing fmt
  • Printing with fmt.Println
From source to output
  1. 1main.goYou write the code
  2. 2go runGo compiles and starts it
  3. 3main()The program starts in main
  4. 4Hello, Go!The text appears

How a Go program runs

Every Go file starts with a package name. A program that you can run lives in package main and starts in the function main. import brings in other packages, such as fmt for printing.

fmt.Println prints its arguments and ends the line. Names that start with a capital letter, such as Println, are the ones a package makes available to other packages.

Key points

  1. A runnable program lives in package main.
  2. It starts in func main.
  3. fmt.Println prints a line of text.

Runnable Go example

Go
Run code →
main.go
package main

import "fmt"

func main() {
	fmt.Println("Hello, Go!")
}
Get a Run button for this example Result of running the code
Hello, Go!

Practice

Open the example in the online compiler, change a value or add a line, and predict the output before you run it. Then break the code on purpose and read the compiler's message.

Finished this lesson?

Mark it as learned to track your progress.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges