Programming

Go

What is Go?

Go was released at Google in 2009. The code stays short, and the program builds into one file that can run without a separate language runtime. It has garbage collection, but not classes or inheritance. Goroutines and channels are how it does many tasks at once.

What is Go used for?

Go is chosen for network services and tools. Large parts of Docker and Kubernetes are written in Go.

  • HTTP API and microservice
  • Command-line program
  • File and network tool
  • Cloud and container project

What can you do with Go?

With Go you write a fast web service, a background job, and a tool that moves from one machine to another the same way. The standard library has an HTTP server, JSON, and a test tool. You do not free memory by hand as in C.

Is Go hard to learn?

The syntax is small, so a first program comes quickly. The harder parts are generics, repeated error checks, and pointers. There is not a framework as large as in Java: you write more yourself or pick a small library.

Advantages and limits

Advantage: a single-file program, clear errors, and strong parallel work. Limit: the desktop window libraries are thin, so it is not the first choice for a GUI or a mobile app. A line can look longer than Java or Kotlin because an error is checked at each step.

Basic ideas

In Go a package, a function, and a struct are the base. An error is usually a returned value, not a chain of exceptions.

  • go.mod stores dependencies
  • A goroutine is a light parallel task
  • A channel carries data between tasks
  • gofmt makes the code look the same

A short example

This program writes one line to the terminal. If the file is main.go, run it with go run main.go.

package main

import "fmt"

func main() {
    fmt.Println("Hello")
}

Common questions

  • Are Go and Golang the same language? Yes. The official name is Go. Golang is the search name.
  • Does Go replace Java? Not everywhere. Often for a network service, not for an Android app.
  • Do you need C before Go? No. C manages memory by hand. Go does not.