Go · Stage 0 – Before You Code · Lesson 6 of 64

Your First Go Program

Write and run a complete Hello World program with every word and symbol explained.

Inside the module folder, create a plain-text file named main.go and enter this program:

Go code

package main

import "fmt"

func main() {
    fmt.Println("Hello, world!")
}
Line-by-line explanation
  1. package main says this file belongs to a runnable program.
  2. import "fmt" brings in Go’s formatting toolbox.
  3. func begins a function definition.
  4. main() is the special starting function for a runnable Go program.
  5. The opening brace begins the function body.
  6. fmt.Println displays one line of text.
  7. The closing brace ends the function body.
Expected output
Hello, world!

This is the program’s output after it runs.

Save the file, then run the current module:

Command

go run .
Line-by-line explanation
  1. go starts the toolchain.
  2. run compiles and immediately runs the program.
  3. . means the current directory.
Expected output
Hello, world!

Go compiles the source in a temporary location, runs it, and displays the message.