Getting Started with Go: Installation, Setup, and Your First “Hello World!” Program
Go, or Golang, is an open-source programming language designed by Google engineers to make it easy to build simple, reliable, and efficient software. It’s known for its strong support for concurrent programming and its simple, clean syntax. In this blog post, we’ll guide you through setting up Golang on your system, creating your first “Hello World!” program, and running it.
Installing Golang
Before you can start working with Go, you’ll need to install the Go programming language on your system.
Windows
- Visit the official Go downloads page at https://golang.org/dl/.
- Download the appropriate installer for your version of Windows (32-bit or 64-bit).
- Run the installer and follow the prompts to complete the installation process. By default, Go will be installed in the
C:\Go
directory, and its binaries will be added to your system'sPATH
.
macOS
- Visit the official Go downloads page at https://golang.org/dl/.
- Download the macOS package file.
- Open the downloaded package file and follow the prompts to complete the installation process. By default, Go will be installed in the
/usr/local/go
directory.
Alternatively, you can use Homebrew to install Go by running:
brew install go
Linux
For Ubuntu, Debian, and their derivatives, use the following commands to install Go:
sudo apt update
sudo apt install golang-go
For Fedora, use the following command:
sudo dnf install golang
For other Linux distributions, refer to the official Go installation instructions at https://golang.org/doc/install.
Setting Up Your Workspace
Once you’ve installed Go, it’s essential to set up your workspace correctly. The recommended workspace structure consists of a directory named go
in your home directory, with three subdirectories: src
, pkg
, and bin
.
- Create the
go
directory in your home directory:
mkdir ~/go
- Create the
src
,pkg
, andbin
subdirectories:
mkdir -p ~/go/{src,pkg,bin}
- Set the
GOPATH
environment variable to thego
directory:
export GOPATH=$HOME/go
Add the above line to your shell’s configuration file (e.g., .bashrc
, .zshrc
, or .bash_profile
) to persist the setting across sessions.
- Add the
bin
subdirectory to yourPATH
:
export PATH=$PATH:$GOPATH/bin
Again, add the above line to your shell’s configuration file to persist the setting.
Creating and Running Your First Go Program
Now that your workspace is set up, let’s create your first Go program:
- Create a directory for your project within the
src
subdirectory:
mkdir -p ~/go/src/hello
- Navigate to the newly created
hello
directory:
cd ~/go/src/hello
- Create a file named
main.go
and open it in your favorite text editor. - Add the following code to main.go:
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}
- Save the file and exit the text editor.
- In your terminal, navigate to the
hello
directory (if you're not already there) and run the following command to build and run your program:
go run main.go
You should see the output:
Hello World!
Congratulations! You’ve successfully created and run your first Go program.
In this blog post, we covered how to install and set up Golang on your system, create a Go workspace, and write and run a simple “Hello World!” program. As you dive deeper into Go, you’ll discover its many powerful features and the ease with which you can build concurrent and efficient applications. Happy coding!