Lesson 1 of 12

Hello, World!

Write and run your first Go program. It shows the three pieces every Go file has: a declaration, and .

Every Go file starts with a package declaration. The main package is the entry point for executables. fmt is the package for formatted I/O. Every Go program that prints something will import fmt.

Go
package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

Save as hello.go and run with go run hello.go. To compile a : go build hello.go, then ./hello. Go compiles fast — fast enough that go run feels instant.

Go
package main

import "fmt"

func main() {
    // Println adds a newline
    fmt.Println("Hello, World!")

    // Printf — C-style formatted output
    name := "Alice"
    age := 30
    fmt.Printf("Name: %s, Age: %d\n", name, age)

    // Sprintf — returns a string instead of printing
    s := fmt.Sprintf("Hello, %s!", name)
    fmt.Println(s)

    // %v — default format (works for anything)
    nums := []int{1, 2, 3}
    fmt.Printf("%v\n", nums)   // [1 2 3]

    // %T — type of value
    fmt.Printf("%T\n", 42)    // int
    fmt.Printf("%T\n", nums)  // []int
}
Finished reading?Mark it done to track your progress.

Tip: use the ← and → keys to move between lessons.