Skip to content

Go Hello World

Every Go program starts with a main package and a main function. This is the entry point of your application.

Your First Program

Create a file named hello.go:

1
2
3
4
5
6
7
package main

import "fmt"

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

How to Run it

You can run Go programs directly from your terminal:

1
2
3
4
5
6
# 1. Run directly (compiled and executed in memory)
go run hello.go

# 2. Build an executable
go build hello.go
./hello

Understanding the Code

  1. package main: This tells the Go compiler that this file should compile as an executable program rather than a shared library.
  2. import "fmt": We import the "fmt" (format) package from the standard library to use its Println function.
  3. func main(): This is the first function that runs when you start your program.
  4. fmt.Println(): Prints text to the console and adds a newline at the end. Note that exported functions in Go always start with a Capital Letter.

The Go Playground

If you don't have Go installed locally yet, you can write and run Go code directly in your browser at go.dev/play.