100% found this document useful (1 vote)
2K views

Golang Cheat Sheet by Golang Dojo

The document provides examples of built-in types, variables, constants, loops, arrays, slices, maps, structs, interfaces, pointers, functions, and goroutines in Golang. It shows declaring and initializing variables, looping through arrays and slices, adding and deleting from maps, defining struct types, implementing interfaces, and passing pointers. It also demonstrates creating goroutines to run functions concurrently.

Uploaded by

Ariel Valladares
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
2K views

Golang Cheat Sheet by Golang Dojo

The document provides examples of built-in types, variables, constants, loops, arrays, slices, maps, structs, interfaces, pointers, functions, and goroutines in Golang. It shows declaring and initializing variables, looping through arrays and slices, adding and deleting from maps, defining struct types, implementing interfaces, and passing pointers. It also demonstrates creating goroutines to run functions concurrently.

Uploaded by

Ariel Valladares
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

G

AN

DO
GOL

JO
BUILT-IN TYPES SWITCH
bool, string, weapon := "Ninja Star"

switch weapon {
Cheat Sheet
int, int8, int16, int32, int64,
[ every golang ninja's secret weapon ]
uint, uint8, uint16, uint32, uint64, uintptr, case "Ninja Star":

rune, byte, fmt.Println("It’s a Ninja Star!")

float32, float64, case "Ninja Sword":

complex64, complex128 fmt.Println("It’s a Ninja Sword!")

VARIABLES
powerLevel := 9001

switch {

var ninja = "Johnny" case powerLevel > 9000:

var level, yoe int = 1, 2 fmt.Println("It’s over...NINE THOUSAND!!!")

var isSkilled bool default:

weapon := "Ninja Star" fmt.Println("It’s a Baby Ninja")

fmt.Println(ninja, level, yoe, isSkilled, weapon)

// Johnny 1 2 false Ninja Star

ARRAYS
// an array is a numbered sequence

CONSTANTS // of elements of a specific length

var evilNinjas [3]string


const dojo string = "Golang Dojo"
fmt.Println(len(evilNinjas))
const powerLevel = 9001

evilNinjas[0] = "Johnny"
const opLevel = 3e20
fmt.Println(evilNinjas)
// a numeric constant has no type
fmt.Println(evilNinjas[0])
// until it's given one
fmt.Println(len(evilNinjas))
fmt.Printf("%T\n", opLevel)

moreEvilNinjas := [3]string{"Andy", "Tommy", "Bobby"}

fmt.Println(moreEvilNinjas)

LOOPS
var missionRewards [2][3]int
isSkilled := true
for i := 0; i < 2; i++ {
for isSkilled {
for j := 0; j < 3; j++ {
fmt.Println("Ready for mission!")
missionRewards[i][j] = i + j
isSkilled = false
}
}
}

for level := 7; level < 9; level++ {

SLICES
fmt.Println(level)

fmt.Println("Leveling up!")

} // a slice, on the other hand, doesn’t need

// to be given a specific length

for { var evilNinjas []string

fmt.Println("I’m a Golang Ninja") fmt.Println(len(evilNinjas))

break evilNinjas = append(evilNinjas, "Tommy")

} fmt.Println(len(evilNinjas))
G
AN

DO
MAPS STRUCTS

GOL

JO
// to create an empty map, use the built-in make type ninja struct {

Cheat Sheet
ninjaLevels := make(map[string]int) name string

ninjaLevels["Johnny"] = 7 level int

ninjaLevels["Tommy"] = 13 } [ every golang ninja's secret weapon ]

fmt.Println(ninjaLevels)

fmt.Println(len(ninjaLevels)) func main() {

fmt.Println(len(ninjaLevels)) fmt.Println(ninja{name: "Bobby", level: 20})

delete(ninjaLevels, "Johnny")

fmt.Println(len(ninjaLevels)) fmt.Println(ninja{name: "Andy", level: 30})

// the optional second return value when getting // omitted fields will be zero-valued

// a value from a map indicates if the key was fmt.Println(ninja{name: "Johnny"})

// present in the map

_, ok := ninjaLevels["Tommy"] tommy := ninja{name: "Tommy", level: 50}

fmt.Println(ok) fmt.Println(tommy.level)

// another option of initializing maps tommy.level = 51

moreNinjaLevels := map[string]int{"Bobby": 8, "Andy": 3} }

fmt.Println(moreNinjaLevels)

INTERFACE
type ninjaWeapon interface{

RANGE attack()

}
evilNinjas:= []string{"Tommy", "Johnny", "Andy"}

for index, evilNinja := range evilNinjas{


type ninjaStar struct{}
fmt.Println("Attacking target", index, evilNinja)

}
func(n ninjaStar) attack() {

fmt.Println("Throwing Ninja Star")


evilNinjasWithLevels:= map[string]int{"Tommy": 2}
}
for evilNinja, level := range evilNinjasWithLevels {

fmt.Printf("%s -> %d\n", evilNinja, level)


type ninjaSword struct{}
}

func(n ninjaSword) attack() {

POINTERS
fmt.Println("Throwing Ninja Sword")

type ninja struct {

name string func main() {

} weapons := []ninjaWeapon{

ninjaStar{},

func main() { ninjaSword{},

tommy := ninja{"Tommy"} }

tommyPointer := &tommy for _, weapon := range weapons {

johnnyPointer := &ninja{"Johnny"} weapon.attack()

var ninjaPointer *ninja = new(ninja) }

} }
G
AN

DO
GOL

JO
Cheat Sheet
FUNCTIONS [ every golang ninja's secret weapon ]

func useWeapon(ninja string, weapon string) string {

return fmt.Sprintf(ninja + "is using " + weapon)


GOROUTINES
}
func attack(target string) {

fmt.Println("Throwing ninja stars at", target)


// multiple return values
}
func isValidLevel(level int) (int, bool) {

if level > 10 {
func main() {
return level, true
go attack("Tommy")
}
time.Sleep(time.Second)
return level, false
}
}

// variadic functions

CHANNELS
func attack(evilNinjas ...string) {

for _, evilNinja:= range evilNinjas{

fmt.Println("Attacking target", evilNinja) func attack(target string, attacked chan bool) {

} time.Sleep(time.Second)

} fmt.Println("Throwing ninja stars at", target)

attacked <- true

func main() { }

usage := useWeapon("Tommy", "Ninja Star")

level, valid := isValidLevel(11) func main() {

smokeSignal := make(chan bool)

fmt.Println(usage, level, valid) evilNinja := "Tommy"

go attack(evilNinja, smokeSignal)

attack("Tommy", "Johnny") fmt.Println(<-smokeSignal)

attack("Tommy", "Johnny", "Andy", "Bobby")

// buffered channels

// if you already have multiple args in a slice, moreSmokeSignal := make(chan bool, 1)

// apply them to a variadic function moreSmokeSignal <- true

// using func(slice...) fmt.Println(<-moreSmokeSignal)

evilNinjas:= []string{"Tommy", "Johnny", "Andy"}

attack(evilNinjas...) // closing channel to prevent deadlocks

moreSmokeSignal <- true

// closures close(moreSmokeSignal)

attackToo := attack for message := range moreSmokeSignal {

attackToo(evilNinjas...) fmt.Println(message)

func() { }

fmt.Println("Attacking Evil Ninjas...") }

}()

FOLLOW US
weekly golang tutorial for beginners

www.golangdojo.com
Golang Dojo

You might also like