No history yet

Go Language Overview

Meet Go

In 2007, engineers at Google felt a growing frustration. The programming languages they used, like C++ and Java, were powerful but had become complex and slow to compile, especially for the massive server software Google builds. They wanted something that combined the performance of a compiled language like C with the readability and ease of use of a dynamic language like Python.

Go is a programming language which is developed by Google in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson.

So, three legendary engineers—Robert Griesemer, Rob Pike, and Ken Thompson (one of the creators of Unix and C)—sat down to design a new language. Their goal was to create a language for the modern era of computing: networked, multicore machines. The result was Go, often called Golang, which was released to the public in 2009.

Lesson image

Simple and Efficient

Go’s biggest feature is arguably what it leaves out. It has a very small specification, meaning there are fewer keywords and language constructs to learn. This simplicity is intentional. It makes code easier to read, write, and maintain, even in large teams.

The language is designed to be clean and orthogonal. For instance, there's only one looping construct: the for loop. No while, no do-while. This reduces ambiguity and makes code more consistent.

This focus on simplicity leads directly to efficiency. Go programs compile incredibly fast, which speeds up the development cycle. The compiled code is a single, standalone binary file that runs directly on the hardware, making it fast and easy to deploy. There's no need for a virtual machine or interpreter.

A Look at the Syntax

Let's peek at some basic Go code. Even if you've never seen it before, you'll likely find it readable.

All Go programs are made up of packages. A program starts running in the main package, inside a function called main.

// A basic Go program
package main

import "fmt"

func main() {
    // Print "Hello, World!" to the console
    fmt.Println("Hello, World!")
}

Variables are declared with the var keyword, or you can use the := short assignment statement inside functions. Go is statically typed, which means the type of a variable is known at compile time, but the compiler can often infer the type for you.

// Declaring a variable
var message string = "This is a message."

// Short assignment (type is inferred)
count := 10

Control structures are straightforward. Here's an if-else statement and the versatile for loop.

// If-else statement
if count > 5 {
    fmt.Println("Count is greater than 5")
} else {
    fmt.Println("Count is not greater than 5")
}

// A simple for loop
for i := 0; i < 3; i++ {
    fmt.Println(i)
}

Functions are defined using the func keyword. They can take zero or more arguments and can return multiple values, a feature often used for returning an error alongside a result.

// This function takes two integers and returns their sum
func add(x int, y int) int {
    return x + y
}

// This function returns two values: a string and an error
func mightFail() (string, error) {
    // For now, it succeeds and returns no error (nil)
    return "Success!", nil
}

Built for Concurrency

One of Go’s most celebrated features is its built-in support for concurrency. Modern computers have multiple cores, but many programming languages make it difficult to use them all at once. Go was designed to make this easy.

concurrency

noun

The ability of different parts or units of a program, algorithm, or problem to be executed out-of-order or in partial order, without affecting the final outcome.

Go uses a concept called goroutines. A goroutine is a lightweight thread managed by the Go runtime. You can think of it as a function that can run alongside other functions. Starting one is as simple as putting the go keyword before a function call.

func say(s string) {
    fmt.Println(s)
}

func main() {
    // Start a new goroutine
    go say("world")
    
    // The main function continues
    say("hello")
}

In the example above, say("world") runs concurrently with say("hello"). They might print in any order. This lightweight approach allows a Go program to manage thousands of goroutines effortlessly.

To communicate between goroutines, Go provides channels. Channels let you send and receive values between goroutines safely, preventing the complex bugs that plague traditional multithreaded programming.

This is a brief look at Go's features. The language is simple by design, compiles quickly into fast binaries, and has first-class support for writing concurrent programs.

Now, let's test your understanding of these core concepts.