1.4 First Program: Hello World (in Go)
Introduction
Writing a "Hello, World!" program is a traditional way to introduce a new programming language. In Go, creating and running your first program is straightforward. This chapter will guide you through writing your first Go program, explaining its structure, and showing you how to compile and run it.
Step 1: Creating Your First Go Program
Go programs are organized into packages. The main
package is the starting point for a Go application, and every executable Go program must have a main
package and a main
function. Let's write a simple Go program that prints "Hello, World!" to the terminal.
package main
: Every Go program must have at least one package, and an executable program must use themain
package. This tells Go that this is the starting point of the application.import "fmt"
: Thefmt
package (short for format) contains functions for formatting input and output. We use it to print to the terminal.func main()
: This is the entry point of a Go program. When you run a Go program, themain()
function is the first thing that executes.fmt.Println("Hello, World!")
: Thefmt.Println()
function prints the text inside the parentheses to the terminal, followed by a new line.
Write the "Hello, World!" Program:Open main.go
in your favorite text editor and write the following Go code:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Let's break this down:
Create the Main Program File:Create a new file named main.go
:
touch main.go
Create a New Directory for Your Project:You can create a new directory for your Go project, either inside or outside the GOPATH
if you are using Go modules. Here’s how to create a directory and navigate into it:
mkdir ~/my-first-go-program
cd ~/my-first-go-program
Step 2: Running the Program
Once you've written your code, you can run it directly using the Go tool.
- The
go run
command compiles and runs the Go program in one step, making it ideal for quick tests and small scripts. - If there are errors in your code, Go will display an error message in the terminal.
go build
compiles your program into a binary file, which can be executed on the operating system without needing the Go toolchain.- This is useful for creating standalone executables that can be distributed and run on other systems.
Compiling and Running the Program with go build
:If you want to compile the program into a binary that you can run independently of the Go toolchain, use the go build
command:
go build
This will generate an executable file (for example, my-first-go-program
on Linux/macOS or my-first-go-program.exe
on Windows) in the same directory. You can run this binary directly:
./my-first-go-program # Linux/macOS
my-first-go-program.exe # Windows
The output should again be:
Hello, World!
Explanation:
Running with go run
:The easiest way to run a Go program is by using the go run
command. This compiles the program in-memory and executes it:
go run main.go
The output should be:
Hello, World!
Explanation:
Step 3: Understanding Go Program Structure
Now that you've written and run your first Go program, let’s take a deeper look at its structure.
Printing Output:The fmt.Println
function prints text to the terminal followed by a new line. It is one of the most common ways to display output in Go:
fmt.Println("Hello, World!")
Other useful functions in the fmt
package include fmt.Printf
(for formatted printing) and fmt.Sprint
(which returns formatted strings instead of printing them directly).
Functions:Functions in Go are declared using the func
keyword. Every Go program starts its execution in the main
function of the main
package:
func main() {
fmt.Println("Hello, World!")
}
The main()
function doesn’t return any values and doesn’t take any arguments in a typical Go program.
Imports:The import
statement is used to include other packages. In this case, we import the fmt
package to use its Println
function:
import "fmt"
The fmt
package provides input/output formatting functions, and it's part of Go’s standard library, so no external dependencies are needed.
Packages:Go code is organized into packages. Every Go source file starts with a package
declaration, and the main
package is special because it defines an executable program. If a program doesn’t have a main
package, Go cannot compile it into an executable.
package main
Step 4: Common Errors and Troubleshooting
Here are some common errors you might encounter when writing and running Go programs, along with ways to fix them:
Case Sensitivity:
Go is case-sensitive. If you try to write fmt.println
instead of fmt.Println
, you will see an error like:
fmt.println undefined (type "fmt" has no field or method "println")
Solution: Use the correct capitalization for functions and variables.
Missing main
Function:
If your program doesn’t have a main
function, Go will complain:
main function not declared in main package
Solution: Declare the main
function as:
func main() {}
Missing main
Package:
If you forget to declare the main
package, Go will give an error:
cannot find package "main"
Solution: Ensure the first line of your file is package main
.
Conclusion
Congratulations! You’ve written and run your first Go program. In this chapter, you learned the basic structure of a Go program, including the main
package, import
statements, and functions. You also explored how to run a Go program using both go run
and go build
.
In the next chapter, we will explore The Go Toolchain and Workspace Structure, which will help you organize and manage larger Go projects efficiently.