Go, often referred to as Golang, is a modern programming language developed by Google. Known for its simplicity, performance, and scalability, Go has become a popular choice for building everything from web servers to cloud-based applications. In this article, we’ll cover how to set up Go on your machine and walk you through writing your first Go program, step by step. By the end, you’ll have a solid understanding of Go’s basics and be ready to dive into more advanced projects.
Step 1: Installing Go
The first step to writing your first Go program is setting up Go on your machine. Go is available for all major operating systems, including macOS, Linux, and Windows. Follow the steps below to install Go on your system:
- On macOS, you can easily install Go using Homebrew:
$ brew install go
- On Windows and Linux, you can download the latest Go executable specific to your operating system from Go’s official website and follow the installation instructions.
Once installed, open your terminal and type go version
to confirm that Go has been installed correctly. You should see something like this:
$ go version go1.23.1 darwin/arm64
Step 2: Writing Your First Go Program
With Go set up, let’s get our hands dirty and write a simple program. We’ll start with the classic “Hello, World!” program, which prints “Hello, World!” to the screen. Follow these steps to create your first Go program:
- Open your favorite code editor and create a new file called
main.go
. - Type the following code into the file:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
In Go, the main.go
file has a special meaning because it can contain the main()
function, which is the entry point for your Go application. Whenever you run your code, the Go compiler looks for this main()
function to start the execution of your program.
Save the file and navigate to its directory in your terminal.
Step 3: Running the Program
Now that you’ve written your first Go program, it’s time to run it. In your terminal, navigate to the directory where your main.go
file is located. Then, run the program using the following command:
$ go run main.go
If everything is set up correctly, you should see this output:
Hello, World!
That’s it—you’ve just written and executed your first Go program!
Conclusion
Congratulations! You’ve just written your first Go program. Go is a powerful language, and now that you’ve set up your environment and learned the basics, you’re ready to explore more advanced concepts. In the next article, we’ll dive into building a web server using Go, where you’ll learn how to handle requests, serve responses, and build a simple backend. Stay tuned!