No history yet

Introduction to Go

What is Go?

Go, often called Golang, is a programming language created at Google in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson. They wanted to create a language that combined the performance of languages like C++ with the simplicity and readability of languages like Python. The goal was to make software development faster, more reliable, and more scalable, especially for large, complex systems.

Lesson image

The designers were frustrated with the slow compile times and complexity of the languages they were using. They envisioned a language that was simple to learn, easy to read, and quick to compile, while still being powerful enough for building modern network services and tools. Go's design philosophy emphasizes clarity and pragmatism over a long list of features.

Key Features

Go has several features that make it a popular choice for developers.

Simplicity: Go has a clean, minimal syntax. With only 25 keywords, it's straightforward to learn, read, and maintain. This focus on simplicity helps development teams stay productive and avoid complex code.

Fast Compilation: Go was designed for speed. Code compiles very quickly, which shortens the development cycle of editing, compiling, and testing.

Statically Typed: Go is a statically typed language, which means variable types are known at compile time. This allows the compiler to catch many common bugs before the program even runs, leading to more robust and reliable software.

Garbage Collection: Go manages memory automatically. Developers don't need to manually allocate and free memory, which is a common source of errors in languages like C. This feature simplifies code and reduces the risk of memory leaks.

Built-in Concurrency: While we won't dive deep into concurrency here, it's one of Go's most famous features. It has excellent built-in support for running multiple tasks simultaneously, making it ideal for building network servers and data pipelines that need to handle many operations at once.

Setting Up Your Environment

Getting started with Go is simple. First, you'll need to install the Go toolchain on your computer.

  1. Visit the official Go website at go.dev.
  2. Download the appropriate installer for your operating system (Windows, macOS, or Linux).
  3. Run the installer and follow the on-screen instructions. The installer will handle the setup for you.

Once the installation is complete, you can verify that Go is ready to use. Open your terminal or command prompt and type 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 darwin/amd64.

Your First Go Program

With Go installed, you're ready to write your first program. It's a tradition in programming to start with a "Hello, World!" application. Create a new file named hello.go and add the following code.

// Your first Go program
package main

import "fmt"

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

Let's break this down:

  • package main: Every Go program is organized into packages. The main package is special; it tells the Go compiler that this is an executable program, not a library.
  • import "fmt": This line imports the fmt package, which is part of Go's standard library. The fmt package contains functions for formatting and printing text, similar to C's printf.
  • func main(): This is the main function where your program's execution begins.
  • fmt.Println("Hello, World!"): This line calls the Println function from the fmt package to print the text "Hello, World!" to the console, followed by a new line.

To run your program, navigate to the directory where you saved hello.go in your terminal and use the go run command:

go run hello.go

You should see the following output:

Hello, World!

Congratulations, you've just written and run your first Go program!