Go Programming Cheatsheet


Comments in go are much like PHP except that it does not support using # as an alternative to //.

// This is a single line comment

 * This is a 
 * multi-line comment 


Variables are typed in Go. E.g. they must be declared as a string, int etc.

Variables are initialized like so:

var x string = "You can assign a variable this way."

var y string
y = "...or you can assign like this..."

z := "... but there is a shorter way."

:= can only be used for initialization. If you have already initialized a variable, you need to use = subsequently.

There is also a shorthand way to initialize multiple variables at once:

var (
    a = 5
    b = 10
    c = 15


Go uses || and && in the same manner as PHP.

if (true && true)
    // This will execute

if (false || true)
    // This will also execute

if (true && false)
    // This will not execute

Outputting to the console

package main
import "fmt"

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


Constants can be defined in the following manner and cannot change value once initialized.

const x string = "Hello, World"

The code below would throw a compilation error:

const x string = "Initial value"
x = "...This will cause an error."

Basic Types


// Integer types
uint8       the set of all unsigned  8-bit integers (0 to 255)
uint16      the set of all unsigned 16-bit integers (0 to 65535)
uint32      the set of all unsigned 32-bit integers (0 to 4294967295)
uint64      the set of all unsigned 64-bit integers (0 to 18446744073709551615)

int8        the set of all signed  8-bit integers (-128 to 127)
int16       the set of all signed 16-bit integers (-32768 to 32767)
int32       the set of all signed 32-bit integers (-2147483648 to 2147483647)
int64       the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)

float32     the set of all IEEE-754 32-bit floating-point numbers
float64     the set of all IEEE-754 64-bit floating-point numbers

complex64   the set of all complex numbers with float32 real and imaginary parts
complex128  the set of all complex numbers with float64 real and imaginary parts

byte        alias for uint8
rune        alias for int32

uint     either 32 or 64 bits
int      same size as uint
uintptr  an unsigned integer large enough to store the uninterpreted bits of a pointer value

Create an Array

The example below shows how to create an array of 5 integers.

var x [5]int


Below shows how to create a map of name/value pairs where the names must be strings and the values must be integers.

myMap := make(map[string]int)
myMap["key"] = 10

For Loop

i := 1

for i <= 10 {
    i = i + 1


switch i {
    case 0: fmt.Println("Zero")
    case 1: fmt.Println("One")
    case 2: fmt.Println("Two")
    case 3: fmt.Println("Three")
    case 4: fmt.Println("Four")
    case 5: fmt.Println("Five")
    default: fmt.Println("Unknown Number")




Stuart is a software developer with a passion for Linux and open source projects.

comments powered by Disqus