Skip to main content

Introduction

Every programmer’s journey begins with “Hello World” - a simple program that displays a message. In Go, this classic first program demonstrates the basic structure of a Go application and how to produce output.

The Code

// Our first program will print the classic "hello world"
// message. Here's the full source code.
package main

import "fmt"

func main() {
	fmt.Println("hello world")
}

Understanding the Structure

Let’s break down each part of this program:

Package Declaration

package main
Every Go file begins with a package declaration. The main package is special - it defines a standalone executable program rather than a library.

Importing Packages

import "fmt"
The import statement brings in the fmt package, which contains functions for formatted I/O (similar to C’s printf and scanf). We need this to print output.

The Main Function

func main() {
	fmt.Println("hello world")
}
The main function is the entry point of the program - execution starts here. We call fmt.Println to print the string “hello world” followed by a newline.
The main function takes no arguments and returns no values. It’s automatically called when you run your program.

Running the Program

To run this program, save it as hello-world.go and execute:
go run hello-world.go
You’ll see:
hello world
You can also build a binary executable with go build hello-world.go, which creates a compiled binary you can run directly.

Key Takeaways

  • Every Go program starts with a package declaration
  • The main package and main() function create an executable program
  • Use import to include packages you need
  • fmt.Println is used for printing output with a newline
  • Go programs can be run directly with go run or compiled with go build

Build docs developers (and LLMs) love