Go Programming Fundamentals
Introduction to Go
What is Go?
Go, often called Golang, is a programming language created at Google in 2007. Its developers, Robert Griesemer, Rob Pike, and Ken Thompson, wanted to build a language that felt simple and efficient, like C, but was better suited for modern computing, especially for programs that need to do many things at once.
Go was designed with a few key principles in mind: readability, speed, and safety. The syntax is clean and straightforward, which makes the code easy to write and maintain. It compiles very quickly, and the resulting programs run fast. Go is also strongly typed, meaning it catches many common errors before you even run your code.
Because of these features, Go has become popular for building backend services, command-line tools, and the kind of software that powers cloud infrastructure. It's known for being reliable and performant.
Setting Up Your Environment
Before you can write Go code, you need to install the Go toolchain on your computer. The official website, go.dev, has installers for Windows, macOS, and Linux. The process is typically quick and simple.
Once it's installed, you can verify that everything is working by opening your terminal or command prompt and typing the following command:
go version
If the installation was successful, you'll see a message displaying the installed version of Go, something like go version go1.22.1 windows/amd64.
Your First Go Program
It's a tradition to start with a "Hello, World!" program. Create a new file named hello.go and add the following code. Don't worry if it looks unfamiliar; we'll break it down line by line.
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Save the file. Now, go back to your terminal, navigate to the directory where you saved hello.go, and run it with this command:
go run hello.go
You should see the output Hello, World! printed to your screen. Congratulations, you've just run your first Go program.
Anatomy of a Go Program
Let's look at the hello.go code again to understand its structure.
package mainEvery Go file starts with a package declaration. Packages are Go's way of organizing code. Themainpackage is special—it tells the Go compiler that this is an executable program, not a library of reusable code.
import "fmt"This line imports thefmtpackage from Go's standard library. Thefmtpackage (short for format) provides functions for formatting text and printing it to the console. We need it for itsPrintlnfunction.
func main()This defines a function namedmain. Just like themainpackage, themainfunction is special. It's the entry point of your program—when you run the code, themainfunction is what gets executed first.
fmt.Println("Hello, World!")Inside ourmainfunction, we call thePrintlnfunction from thefmtpackage. This function prints the text "Hello, World!" to the console, followed by a new line. The syntaxfmt.Printlntells Go to use thePrintlnfunction from thefmtpackage.
These four lines represent the basic structure of a simple, runnable Go application. You'll see this pattern of package, import, and func main in every executable program you write.
Time to check your understanding.
What were the key principles guiding the design of the Go language?
Which command is used to verify that the Go toolchain is installed correctly and to display its version?
Now that you have the basics down, you're ready to explore more of what Go has to offer.
