a8m /golang-cheat-sheet

An overview of Go syntax and features.

★ 8,842⑂ 1,106👁 200⑃ 56● 27▤ 0
Created 2014-02-13
+0
Daily
8,842
stars
1,106
forks
200
watchers
56
prs
27
issues
0
releases

README

Go Cheat Sheet

 Index
1. Basic Syntax
2. Operators
     Arithmetic
     Comparison
     Logical
     Other
3. Declarations
4. Functions
     Functions as values and closures
     Variadic Functions
5. Built-in Types
6. Type Conversions
7. Packages
8. Control structures
     If
     Loops
     Switch
9. Arrays, Slices, Ranges
     Arrays
     Slices
     Operations on Arrays and Slices
10. Maps
11. Structs
12. Pointers
13. Interfaces
14. Embedding
15. Errors
16. Concurrency
     Goroutines
     Channels
     Channel Axioms
17. Printing
18. Reflection
     Type Switch
     Examples
19. Snippets
     Files Embedding
     HTTP Server

 Credits

Most example code taken from A Tour of Go, which is an excellent introduction to Go.
If you're new to Go, do that tour. Seriously.

 Go in a Nutshell

 Imperative language
 Statically typed
 Syntax tokens similar to C (but less parentheses and no semicolons) and the structure to Oberon-2
 Compiles to native code (no JVM)
 No classes, but structs with methods
 Interfaces
 No implementation inheritance. There's type embedding, though.
 Functions are first class citizens
 Functions can return multiple values
 Has closures
 Pointers, but not pointer arithmetic
 Built-in concurrency primitives: Goroutines and Channels

 Basic Syntax

 Hello World
File hello.go:
go
package main

import "fmt"

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

$ go run hello.go

 Operators
 Arithmetic
|Operator|Description|
|--------|-----------|
|+|addition|
|-|subtraction|
||multiplication|
|/|quotient|
|%|remainder|
|&|bitwise and|
|\||bitwise or|
|^|bitwise xor|
|&^|bit clear (and not)|
||right shift|

 Comparison
|Operator|Description|
|--------|-----------|
|==|equal|
|!=|not equal|
||greater than|
|=|greater than or equal|

 Logical
|Operator|Description|
|--------|-----------|
|&&|logical and|
|\|\||logical or|
|!|logical not|

 Other
|Operator|Description|
|--------|-----------|
|&|address of / create pointer|
||dereference pointer|
| package rand)
 Upper case identifier: exported (visible from other packages)
 Lower case identifier: private (not visible from other packages)

 Control structures

 If
go
func main() {
	// Basic one
	if x  10 {
		return x
	} else if x == 10 {
		return 10
	} else {
		return -x
	}

	// You can put one statement before the condition
	if a := b + c; a  42:
            fmt.Println("Greater")
    }

    // cases can be presented in comma-separated lists
    var char byte = '?'
    switch char {
        case ' ', '?', '&', '=', '', '+', '%':
            fmt.Println("Should escape")
    }


 Arrays, Slices, Ranges

 Arrays
go
var a [10]int // declare an int array with length 10. Array length is part of the type!
a[3] = 42     // set elements
i := a[3]     // read elements

// declare and initialize
var a = [2]int{1, 2}
a := [2]int{1, 2} //shorthand
a := [...]int{1, 2} // elipsis - Compiler figures out array length


 Slices
go
var a []int                              // declare a slice - similar to an arra

Top issues

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON