Your First Go Program: "Hello, World!"
Embarking on your journey with Go (Golang) begins with a fundamental tradition: writing a "Hello, World!" program. This simple program serves as your initial interaction with the Go compiler and runtime, demonstrating the basic structure of a Go application.
Understanding the "Hello, World!" Structure
A typical "Hello, World!" program in Go involves a few key components: a package declaration, an import statement, and a main function. Let's break down each part.
Every Go program starts with a `package main` declaration.
The package main
declaration tells the Go compiler that this package should be compiled into an executable program, rather than a shared library.
In Go, code is organized into packages. The package
keyword at the beginning of a file declares which package the file belongs to. For executable programs, this package must be main
. The main
package is special because it tells the Go toolchain that this package should be compiled into an executable binary. Without package main
, your code would be treated as a library that other programs could import.
The `import "fmt"` statement brings in the formatting package.
The fmt
package provides functions for formatted I/O (input/output), such as printing text to the console.
To use functionality from other packages, you need to import
them. The fmt
package is part of Go's standard library and is essential for tasks like printing output. The import
keyword is followed by the name of the package in double quotes. When you need to use functions like Println
from the fmt
package, you must import it first.
The `func main()` is the entry point of your program.
The main
function is where the execution of your Go program begins. It's the first function that runs when you execute the compiled binary.
Every executable Go program must have a main
function. This function is the starting point of your program's execution. When you run your Go program, the Go runtime looks for and calls the main
function within the main
package. The func
keyword declares a function, main
is the function name, and ()
indicates that it takes no arguments. The curly braces {}
enclose the body of the function, containing the code that will be executed.
Inside the
main
fmt.Println("Hello, World!")
Println
fmt
Here's the complete "Hello, World!" program in Go:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
This code demonstrates the fundamental structure: package declaration, import statement, and the main function containing the print statement. The fmt.Println
function is a key tool for outputting information.
Text-based content
Library pages focus on text content
Running Your First Go Program
To run this program, you'll need to have Go installed. Save the code above in a file named
main.go
go run main.go
This command compiles and runs your Go program. You should see the output:
Hello, World!
The go run
command is a convenient way to execute Go source files without explicitly building an executable first. It's perfect for quick testing and learning.
package main
in a Go program?It declares that the package should be compiled into an executable program.
The fmt
package.
The main
function.
Learning Resources
The official Go tour provides an interactive introduction to the language, starting with the classic 'Hello, World!' example.
This site offers practical, runnable examples of Go programming concepts, including a clear explanation of 'Hello, World!'.
Dive into the official Go language specification to understand the fundamental concepts of packages and their role in Go programs.
A blog post detailing how packages work in Go, including importing and using them, with practical examples.
A beginner-friendly video tutorial that walks through writing and running your very first Go program.
The official home of the Go programming language, offering documentation, downloads, and community resources.
Explore the official documentation for the `fmt` package, which is crucial for input and output operations in Go.
An essential guide from the Go team on setting up your Go workspace and writing your first Go programs.
A comprehensive overview of the Go programming language, its history, features, and use cases.
Understand the functionality and usage of the `go run` command, which compiles and runs Go programs.