No history yet

Introduction to Go

Meet Go

Go, often called Golang, is a programming language born out of frustration at Google. Around 2007, engineers were struggling with massive, complex codebases written in languages like C++ and Java. Compiling code took ages, managing dependencies was a headache, and writing concurrent programs that could take advantage of modern multi-core processors was incredibly difficult.

Lesson image

So, a team of distinguished engineers—Robert Griesemer, Rob Pike, and Ken Thompson—decided to build a new language from scratch. They wanted something that felt like a dynamic language like Python for development speed but had the performance and safety of a compiled language like C++.

The result was Go. It’s designed around a few key principles:

  • Simplicity and Readability: Go has a small, clean syntax. The idea is that code is read far more often than it's written, so making it easy to understand is a top priority. It intentionally leaves out complex features found in other languages.
  • Fast Compilation: Go compiles extremely quickly, making the development cycle feel fast and interactive.
  • Built-in Concurrency: Go makes it easy to write programs that do multiple things at once using features called 'goroutines' and 'channels'. This is a huge advantage for building networked services and high-performance applications.

Setting Up Your Environment

Getting started with Go is straightforward. The first step is to install the Go toolchain on your computer. You can download the official installer for your operating system—Windows, macOS, or Linux—directly from the Go website at go.dev.

Run the installer and follow the on-screen instructions. It will handle setting up everything you need, including adding the Go command to your system's PATH. Once the installation is complete, you can verify it worked by opening a terminal or command prompt and running this command:

go version

If everything is set up correctly, you'll see a message displaying the installed version of Go, like go version go1.22.1 linux/amd64. Now you're ready to write some code.

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 about understanding every piece just yet; we'll break it down right after.

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

Let's walk through this code line by line:

  • package main: Every Go program is made up of packages. The main package is special. It tells the Go compiler that this is an executable program that can be run, not a library to be used by other programs.
  • import "fmt": This line imports the fmt package, which is part of Go's standard library. The name fmt is short for "format," and it contains functions for formatting and printing text, including the one we use below.
  • func main(): This defines a function named main. The main function is the entry point of your program. When you run the program, the code inside this function is what gets executed.
  • fmt.Println("Hello, World!"): Here, we're calling the Println function from the fmt package. As its name suggests, it prints a line of text to the console. We pass it the string "Hello, World!" to be printed.

Notice the . in fmt.Println. This is the standard way to access a function or variable from an imported package in Go.

With the code saved in hello.go, navigate to that directory in your terminal. To run the program, use the go run command:

go run hello.go

You should see the output Hello, World! printed to your terminal. Congratulations, you've just written and run your first Go program.

Quiz Questions 1/5

What was the primary motivation for creating the Go language at Google?

Quiz Questions 2/5

In a standalone, executable Go program, the entry point package must be named package main.

You've taken the first steps into the world of Go, from understanding its origins at Google to running a simple program. You're now set up and ready to explore more of what the language has to offer.