Go Programming Series: This lesson is part of our comprehensive Complete Go Tutorial: Learn Go (Golang) from Beginner to Advanced.

Go Hello World Program: Write and Run Your First Go Code

The Hello World program is the first small application most Go learners write. It is simple, but it introduces several ideas that become important throughout Go development: packages, imports, the main function, source files, the go command, compilation, execution, and basic program output.

This guide shows how to create a Go Hello World program from scratch, run it in different ways, understand every line, build an executable, troubleshoot common problems, and use the exercise as a bridge into Go syntax and core programming concepts.

What You Will Learn

  • How a minimal Go program is structured
  • What package main means
  • What func main does
  • Why fmt is imported
  • How to create and run a Go source file
  • The difference between go run and go build
  • How to build a standalone executable
  • Common beginner errors and how to troubleshoot them
  • What to learn after Hello World

1. What Is a Go Hello World Program?

A Hello World program is a deliberately small program that produces a visible result. In Go, the smallest useful example normally contains a main package, an imported package for printing, and a main function.

package main

import "fmt"

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

When this program runs, it prints Hello, World! to the standard output.

2. Understanding the Program Line by Line

package main

package main declares that this source file belongs to the main package. A Go program intended to produce an executable normally uses package main.

import "fmt"

The import statement makes the fmt package available to the file. The fmt package provides formatted I/O functions, including fmt.Println.

func main()

The main function is the entry point used when a package main program is executed. The statements inside this function run as the program starts.

fmt.Println("Hello, World!")

fmt.Println writes the supplied value to standard output and adds a newline after it.

3. What Do You Need Before Writing Hello World?

Requirement Purpose
Go installation Provides the compiler and go command.
Text editor or IDE Lets you create and edit the .go file.
Terminal Lets you run go commands directly.
A working directory Keeps the practice files organized.

You do not need a complex project, database, web framework, or external library for this first program.

4. Create Your First Go File

Create a new directory for the exercise and create a file named main.go.

mkdir hello-go
cd hello-go

Create main.go and enter:

package main

import "fmt"

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

5. Run the Program with go run

The quickest way to execute a small Go program during learning is go run.

go run main.go

Expected output:

Hello, World!

The go command handles the build-and-run workflow for this development command. It is convenient when you want to test a source file without manually creating the final executable first.

6. Build the Program with go build

go build compiles the program into an executable for the target environment.

go build

After a successful build, the generated executable can be run according to your operating system and shell.

Why Learn Both Commands?

Command Use It When
go run You are experimenting, learning, or quickly testing source code.
go build You want to compile the program and work with the resulting executable.
go test You want to run automated tests.
go fmt You want to apply standard Go formatting.

7. Build an Explicit Output Name

You can specify the output file name with the -o option.

go build -o hello
./hello

On Windows, the generated executable is typically handled with the platform's executable conventions. The exact command used to launch it depends on the shell and operating system.

8. Create a Go Module

For a very small one-file experiment, you may encounter examples that run without first creating a module. For normal project development, however, learning Go modules early is useful.

go mod init example.com/hello

This creates a go.mod file that records the module path and Go module metadata. The module path is chosen according to the project and its eventual import or repository structure.

go run .

Using go run . is especially useful once the directory contains a complete Go package rather than a single file.

9. Hello World with a Variable

Once the basic program works, introduce one concept at a time.

package main

import "fmt"

func main() {
    message := "Hello, Go!"
    fmt.Println(message)
}

The := syntax declares and initializes a local variable. This small change turns the example into a bridge toward Go variables.

10. Hello World with a Function

Functions let you move reusable behavior outside main.

package main

import "fmt"

func greeting() string {
    return "Hello, Go!"
}

func main() {
    fmt.Println(greeting())
}

Now the program introduces function declarations and return values, which are central to Go programming.

11. Hello World with User Input

A next step is to accept input from the user.

package main

import "fmt"

func main() {
    var name string
    fmt.Print("Enter your name: ")
    fmt.Scanln(&name)
    fmt.Printf("Hello, %s!\n", name)
}

This example introduces variables, pointers as arguments, formatted output, and basic input. For production applications, input handling usually needs more validation than this small example provides.

12. Hello World as an HTTP Server

After learning the basic program structure, you can see how Go's standard library scales from small programs to network services.

package main

import (
    "fmt"
    "net/http"
)

func hello(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "Hello, Go HTTP server!")
}

func main() {
    http.HandleFunc("/", hello)
    http.ListenAndServe(":8080", nil)
}

This is no longer a beginner-sized first exercise, but it demonstrates why learning the fundamentals matters: the same package-and-function model is used inside a real HTTP server.

13. Common Hello World Errors

Error: go is not recognized or command not found

The Go command may not be installed or may not be available through your PATH. Verify the installation with:

go version

If that command fails, return to the Go installation and environment setup steps before troubleshooting the program itself.

Error: expected 'package'

A Go source file must begin with a package declaration. Check the first line of main.go.

package main

Error: imported and not used

Go reports unused imports. If you import fmt but do not call anything from fmt, remove the import or use the package.

import "fmt"

For example, fmt.Println uses the imported package.

Error: undefined: fmt

This usually means the fmt package was not imported correctly or the identifier was written incorrectly.

Error: function main is missing

An executable package main needs a main function. Check that the function is spelled exactly as main and belongs to package main.

func main() {
    // program statements
}

14. Why Does Go Care About Formatting?

Go includes a standard formatting tool, gofmt. Consistent formatting is part of the normal Go development workflow.

gofmt -w main.go

The command rewrites the file using Go's standard formatting rules. In many projects, editors and CI workflows run formatting automatically.

15. How to Explore the Program Yourself

Do not stop after copying Hello World. Change one thing at a time and observe what happens.

  • Change the message.
  • Store the message in a variable.
  • Create a greeting function.
  • Ask the user for a name.
  • Print two or more values.
  • Use an if statement to change the greeting.
  • Create a slice of names and loop over it.
  • Move the greeting function into another file in the same package.
  • Turn the program into a small HTTP endpoint.

16. A Useful First-Program Experiment

package main

import "fmt"

func main() {
    names := []string{"Asha", "Rahul", "Maya"}

    for _, name := range names {
        fmt.Printf("Hello, %s!\n", name)
    }
}

This tiny example introduces a slice, range, a loop variable, and formatted output. It is a good transition from the first program into Go's core syntax and data structures.

17. Hello World Learning Checklist

  • Create a main.go file.
  • Understand package main.
  • Understand the main function.
  • Import fmt correctly.
  • Run the program with go run.
  • Build the program with go build.
  • Know the difference between running and building.
  • Create a module for normal project work.
  • Modify the example instead of only copying it.
  • Be ready to learn variables, types, and Go syntax next.

18. What Should You Learn After Hello World?

Hello World is the transition point between environment setup and actual Go programming. Continue in a deliberate order.

Next Topic Why Learn It
Go Syntax Understand the rules and structure of Go code.
Comments Document code and learn Go documentation conventions.
Variables Store and manipulate values.
Data Types Understand the kinds of values a program can represent.
Type Conversion Move values between compatible types explicitly.
Operators Perform calculations and comparisons.
Input and Output Build interactive console programs.
Conditions and Loops Create real program logic.

19. Frequently Asked Questions

What is the simplest Go Hello World program?

A minimal executable program is package main with an imported fmt package and a main function that calls fmt.Println("Hello, World!").

How do I run a Go program?

For a simple source file, use go run main.go. For a module-based project, go run . is commonly used from the module directory.

What is the difference between go run and go build?

go run provides a convenient development workflow for compiling and running code. go build compiles the program into an executable artifact.

Do I need go.mod for Hello World?

A tiny one-file experiment can be run in simple circumstances without a module, but normal Go projects should use Go modules. Learning go.mod early is useful for real development.

Why is package main required?

An executable Go program uses package main. The package identifies the program as an executable package rather than a reusable library package.

Why does Go reject unused imports?

Unused imports are usually a sign of unnecessary dependencies in a source file. Go treats them as compile-time errors so source files remain clean.

Can I write Hello World in an IDE?

Yes. An IDE can run the same Go toolchain commands behind the scenes. Beginners should still learn the command-line workflow because it helps with troubleshooting and automation.

Conclusion

The Go Hello World program is small, but it introduces the core workflow used throughout Go development: create source code, organize it into packages, use imports, define functions, run the program, and build an executable.

Once this example works, move beyond copying it. Change the program, add variables and functions, process input, and experiment with loops. Your next step is to learn Go syntax and core concepts so that you can turn the first successful program into useful software.