Last modified: Jan 21, 2026 By Alexander Williams
Your First Go Program: Hello World Tutorial
Starting with a new language can be exciting. The classic first step is the "Hello, World!" program. It is a simple but vital ritual.
This guide walks you through creating your first Go program. You will write code, understand its parts, and run it. Let's begin your journey into Go programming.
Prerequisites
You need Go installed on your computer. Visit the official Go website. Download and run the installer for your operating system.
Verify the installation by opening a terminal. Type go version. You should see the installed version number. This confirms Go is ready.
go version
go version go1.21.0 linux/amd64
Setting Up Your Workspace
Create a new directory for your Go projects. This keeps your code organized. Use your terminal or file explorer.
Inside, create a folder for this specific program. For example, name it "hello-world". Navigate into this folder using the terminal.
mkdir -p ~/go-projects/hello-world
cd ~/go-projects/hello-world
Writing the Hello World Code
Create a new file named main.go. The ".go" extension is required for Go source files. You can use any text editor.
Open the file and type the following code. We will examine each line in detail after you see it run.
// This is your first Go program.
package main
import "fmt"
func main() {
// Print a message to the console.
fmt.Println("Hello, World!")
}
Understanding the Code Structure
Let's break down the program line by line. Understanding this is key to learning Go.
The Package Declaration
The first line is package main. Every Go file starts with a package declaration. It defines the file's namespace.
The main package is special. It tells Go this is an executable program, not a library. Executables must have a package named main.
The Import Statement
The line import "fmt" brings in external code. Here, we import the "fmt" package. It stands for "format".
This package contains functions for formatted input and output. We use it to print text to the screen. Imports give your program more power.
The Main Function
The func main() defines the main function. This is the program's entry point. Execution starts here when you run the program.
The empty parentheses mean it takes no arguments. The curly braces {} enclose the function's body. All code inside runs.
The Print Statement
Inside the function, we have fmt.Println("Hello, World!"). This calls the Println function from the "fmt" package.
It prints the text "Hello, World!" to the standard output (your terminal). The function adds a newline at the end automatically.
Running Your Go Program
Go to your terminal. Ensure you are in the directory containing main.go. Now, run the program with the go run command.
go run main.go
The command compiles the code in memory and executes it immediately. You should see the output right away.
Hello, World!
Congratulations! You have successfully run your first Go program. This simple output is a major milestone.
Alternative: Building an Executable
The go run command is great for quick testing. You can also build a standalone binary file. Use the go build command.
go build main.go
This creates an executable file in the same directory. On Windows, it will be "main.exe". On Unix-like systems, it will be "main".
You can run this file directly from your terminal or file manager. It does not require the Go toolchain to be installed on the machine.
# On Linux/macOS
./main
# On Windows (in Command Prompt)
main.exe
Common Errors and Solutions
Beginners often encounter a few common issues. Here's how to fix them.
File Not in main Package
If your package is not named "main", go run will fail. The error will say "package main is not in main".
Solution: Ensure the first line of your file is package main. This is non-negotiable for executables.
Missing Import or Typo
If you misspell "fmt" or forget the import, you get an error. Go will not find the Println function.
Solution: Double-check the import statement. It must be exactly import "fmt". Case matters in Go.
Function main is Undefined
Go needs a main function to run. If it's missing or named incorrectly, the tool will complain.
Solution: Define a function exactly as func main(). Spelling and case are crucial here as well.
Experimenting with Your Code
Now that it works, try making changes. This is the best way to learn. Edit the message inside the quotes.
Change "Hello, World!" to "Greetings from Go!". Save the file and run it again. See the new output appear.
You can also try using fmt.Print instead of fmt.Println. Notice it does not add a newline. The next terminal prompt will appear on the same line.
package main
import "fmt"
func main() {
fmt.Print("Hello, ")
fmt.Print("World!")
}
Hello, World!$
Next Steps in Your Go Journey
You've mastered the first step. Where do you go from here? The world of Go programming is vast and powerful.
Learn about variables, data types, and control structures like loops and conditionals. Explore how to write functions and organize code into multiple files.
Go has excellent support for concurrency and web services. These are some of its greatest strengths. Your simple start is the foundation for complex projects.
Conclusion
Writing "Hello, World!" in Go is a straightforward process. You set up a workspace, wrote a simple program, and executed it.
You learned about packages, imports, and the main function. You also saw how to build a binary and troubleshoot common errors.
This first program opens the door. You now have the basic tools to explore more of the Go language. Keep coding and building.