Programming

Go Cheatsheet

Go is a fast, statically typed language built for simplicity and concurrency. This cheatsheet covers its core syntax.

Go (Golang) is a statically typed, compiled language designed for simplicity, performance, and built-in concurrency. This cheatsheet covers the core syntax.

Program Structure

Every Go file belongs to a package.

package main

import "fmt"

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

Variables

Declaration and short form.

var name string = "Ada"
age := 36              // short declaration
const pi = 3.14
var x, y = 1, 2

Types

Common built-in types.

var i int = 42
var f float64 = 3.14
var b bool = true
var s string = "go"
nums := []int{1, 2, 3}         // slice
m := map[string]int{"a": 1}    // map

Functions

Multiple return values are idiomatic.

func add(a, b int) int {
    return a + b
}

func divide(a, b int) (int, error) {
    if b == 0 {
        return 0, fmt.Errorf("divide by zero")
    }
    return a / b, nil
}

Control Flow

Conditionals and loops.

if age > 18 {
    fmt.Println("adult")
}

for i := 0; i < 5; i++ {
    fmt.Println(i)
}

for i, v := range nums {
    fmt.Println(i, v)
}

Structs

Custom data types.

type User struct {
    Name string
    Age  int
}

u := User{Name: "Ada", Age: 36}
fmt.Println(u.Name)

Goroutines & Channels

Built-in concurrency.

go doWork()             // run concurrently

ch := make(chan int)
go func() { ch <- 42 }()
val := <-ch             // receive

Error Handling

Explicit, value-based errors.

result, err := divide(10, 2)
if err != nil {
    log.Fatal(err)
}
fmt.Println(result)

Go's simplicity and concurrency make it great for backends, CLIs, and cloud tools. Learn the basics, then explore goroutines, interfaces, and the standard library.

For full documentation, see https://go.dev/doc/

Promote your content

Reach over 400,000 developers and grow your brand.

Join our developer community

Hang out with over 4,500 developers and share your knowledge.