Whether you're new to programming or just diving into Go, creating a "Hello, World!" program is an excellent first step. This guide will walk you through writing, building, and running a simple Go application on Linux. Along the way, we’ll troubleshoot common issues to ensure you have a smooth start.
1. Setting Up Go on Linux
Before writing any code, make sure you have Go installed on your system.
Installing Go on Linux
- Download Go: Go to golang.org/dl and download the latest version for Linux.
Set Up the Path: Add Go to your PATH by adding the following lines to ~/.profile:
export PATH=$PATH:/usr/local/go/bin
Apply the changes by running:
source ~/.profile
Extract Go:
sudo tar -C /usr/local -xzf go1.xx.linux-amd64.tar.gz
(Replace go1.xx.linux-amd64.tar.gz with the version you downloaded.)
Verify the Installation
Run:
go version
You should see output similar to go version go1.xx linux/amd64, confirming Go is installed.
Or just install golang with APT package manager by running the following commands:
sudo apt update
sudo apt install golang2. Creating the "Hello, World!" Application
Step 1: Set Up Your Project Directory
Create a directory for the project and navigate into it:
mkdir hello_world_go
cd hello_world_go
Step 2: Write the Code
Create a new file named main.go:
touch main.go
Open main.go in a text editor and add the following code:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Code Explanation
package main: Marks the program as an executable rather than a reusable package.import "fmt": Imports thefmtpackage, which contains functions for formatting text.func main(): Declares themainfunction as the entry point for the program.fmt.Println("Hello, World!"): Prints "Hello, World!" to the console.
3. Building and Running the Program
Step 1: Build the Program
To compile the code into an executable, use:
go build main.go
This command generates an executable file named main in the current directory.
Step 2: Run the Program
Execute the file:
./main
You should see:
Hello, World!
Running Without Building
Alternatively, you can use go run to compile and execute the program in one step:
go run main.go
This command does not save a compiled file, but it’s convenient for quick testing.
4. Troubleshooting Common Issues
Issue 1: go: command not found
Solution: If the go command isn’t recognized, ensure Go is in your PATH:
Add Go to your PATH by adding export PATH=$PATH:/usr/local/go/bin to ~/.profile, and then run:
source ~/.profile
Verify the installation path:
which go
Issue 2: cannot find package "fmt"
This usually means that the GOROOT (Go's installation path) or GOPATH (where Go expects projects) isn’t configured correctly.
Solution:
Apply the changes:
source ~/.profile
Check GOPATH:
echo $GOPATH
If it’s empty, set it to a workspace, like:
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
Check GOROOT:
echo $GOROOT
If it’s not set, add the following to ~/.profile:
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
Issue 3: Permission Denied when Running the Executable
If you get a permission denied error, it means the file doesn’t have execute permissions.
Solution:
Add execute permissions to the compiled file:
chmod +x main
./main
Issue 4: Running in Docker or Remote Machines
If you want to run this on a remote Linux environment or Docker, ensure that:
- The Go environment variables (
GOROOTandGOPATH) are set correctly. - Necessary network ports are open if connecting via SSH.
5. Tips for Working with Go on Linux
- Running Tests: If your code expands, test it using Go’s testing tools. Start by adding
_test.gofiles and usego testto run tests. - Using IDEs: Many IDEs, like VSCode or GoLand, offer great Go support, including code completion, formatting, and error highlighting.
Use go fmt: Go has a built-in formatting tool that keeps your code clean and consistent:
go fmt main.go
6. Beyond "Hello, World!": Next Steps
Learning Go can open doors to systems programming, server applications, and microservices. After mastering the basics:
- Explore the Go standard library.
- Try building a web server using Go’s
net/httppackage. - Look into Go routines and channels for concurrent programming.