Go Programming: First Program - "Hello, World!"
This guide will walk you through creating your first Go program, the classic "Hello, World!" example. It's a fundamental step in learning any new programming language.
1. Setting up Your Go Environment (If you haven't already)
Before you begin, ensure you have Go installed and configured on your system. You can download the latest version from the official Go website: https://go.dev/dl/
After installation, verify it by opening a terminal or command prompt and running:
go version
This should display the installed Go version. You'll also need to set up your GOPATH and GOROOT environment variables. Modern Go versions (1.11 and later) often handle this automatically with Go Modules, but it's good to be aware of them.
2. Creating the Go Source File
Open a text editor (like VS Code, Sublime Text, Atom, or even a simple notepad) and create a new file named hello.go. The .go extension is crucial for Go source files.
3. Writing the Code
Inside hello.go, type the following code:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Let's break down this code:
package main: This line declares that the code belongs to themainpackage. Themainpackage is special; it's where the execution of your program begins. Every executable Go program must have amainpackage.import "fmt": This line imports thefmtpackage. Packages in Go are collections of related functions and data types. Thefmtpackage provides functions for formatted input and output, including printing to the console.func main() { ... }: This defines themainfunction. This is the entry point of your program. When you run the program, the code inside themainfunction is executed.fmt.Println("Hello, World!"): This is the core of the program. It calls thePrintlnfunction from thefmtpackage.Printlnprints the given string ("Hello, World!") to the console, followed by a newline character.
4. Running the Program
Open a terminal or command prompt, navigate to the directory where you saved hello.go, and run the following command:
go run hello.go
This command compiles and runs the hello.go file. You should see the following output in your terminal:
Hello, World!
5. Building an Executable (Optional)
Instead of running the code directly with go run, you can build an executable file. This creates a standalone program that you can run without the go command.
Run the following command:
go build hello.go
This will create an executable file named hello (or hello.exe on Windows) in the same directory. You can then run the executable directly:
- Linux/macOS:
./hello - Windows:
hello.exe
This will also print "Hello, World!" to the console.
Key Takeaways:
- Go programs are organized into packages.
- The
mainpackage is the entry point for executable programs. - The
mainfunction is where execution begins. - The
fmtpackage is commonly used for input and output. go runcompiles and runs the code directly.go buildcreates a standalone executable file.
This simple program demonstrates the basic structure of a Go program. From here, you can start exploring more complex concepts and building more sophisticated applications. Good luck!