Hello World in Go — Breaking Down Your First Program

If you’re new to Go (also called Golang), the best way to start is by writing the classic Hello World program. It’s simple, yet it teaches you the basic structure of every Go program.
Let’s walk through it step by step.

Writing Your First Go Program

Here’s the complete code:

package main

import "fmt"

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

Now, let’s break it down.


1. package main

In Go, every file begins with a package declaration.
The main package tells the Go compiler that this is an executable program, not a shared library.
When you run a Go program, it always looks for the main package and then the main() function inside it.


2. import "fmt"

The import keyword lets you use code from other packages.
Here, we’re importing the fmt package, which stands for format.
It provides functions for formatting text — and in our case, printing it to the console.


3. func main()

This is the main function — the starting point of a Go program.
When you run your code, Go looks for main() and begins execution from here.


4. fmt.Println("Hello, World!")

Inside the main function, we call fmt.Println.

  • Println stands for Print Line.
  • It prints the text inside the quotes and moves the cursor to a new line.

So when you run the program, it simply prints:

Hello, World!

Running the Program

Save the code in a file called main.go.

Open your terminal and run:

go run main.go

You’ll see:

Hello, World!

Congratulations! You just wrote and ran your first Go program.