Show Menu
Cheatography

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

This is a draft cheat sheet. It is a work in progress and is not finished yet.

WaitGroups

func generateRandomNumber(ch chan<- int, id int, wg *sync.WaitGroup) {
    defer wg.Done()

    rand.Seed(time.Now().UnixNano() + int64(id))
    randomNumber := rand.Intn(100)

    ch <- randomNumber
}

func main() {
    var wg sync.WaitGroup
    ch := make(chan int, 100)

    runtime.GOMAXPROCS(3)

    for i := 1; i <= 100; i++ {
        wg.Add(1)
        go generateRandomNumber(ch, i, &wg)
    }

    go func() {
        wg.Wait()
        close(ch)
    }()

    for randomNumber := range ch {
        fmt.Println(randomNumber)
    }
}

Arrays

func mean(tab [5]int) (meanVal float64) {
  // for index, value := range collection
  for _, value := range tab {
    meanVal+= (float64)(value)
  }
  meanVal /= (float64)(len(tab))
  return
}

func main() {
  var table = [5]int{3, 4, 8, 9, 2}
  m := mean(table) // pass by value
  fmt.Printf("result= %f\n", m)
}

Maps

func main() {
    m := make(map[string]int)

    m["k1"] = 7
    m["k2"] = 13

    fmt.Println("map:", m) //map: map[k1:7 k2:13]

    v1 := m["k1"]
    fmt.Println("v1:", v1) //v1: 7

    v3 := m["k3"]
    fmt.Println("v3:", v3) //v3: 0

    fmt.Println("len:", len(m)) //len: 2

    delete(m, "k2")
    fmt.Println("map:", m) //map: map[k1:7]

    _, prs := m["k2"] //second return val to see if key is present
    fmt.Println("prs:", prs) //prs: false

    n := map[string]int{"foo": 1, "bar": 2}
    fmt.Println("map:", n) //map: map[bar:2 foo:1]
}

Method

func (pt *Point) norm() float64 {
  return math.Sqrt(pt.xpt.x + pt.ypt.y)
}

Range

for i, num := range nums {
   fmt.Println("index: ", i, " num: ", num)
}

Range

for i, num := range nums {
   fmt.Println("index: ", i, " num: ", num)
}
 

Iota

const (
  ON = iota
  OFF = iota // optional here
  OTHER = iota
)
fmt.Println(ON, OFF, OTHER) // Will print: 0 1 2

Functions as Parameters

func main() {
  a := Point{2.,4.}
  b := Point{5.,9.}
  dist := calc(a,b,Distance)
  fmt.Printf("result= %f\n", dist)
  dist = calc(a,b,
    func(p Point, q Point)float64{
      return math.Abs(p.x-q.x)+math.Abs(p.y-q.y)
  })
  fmt.Printf("result= %f\n", dist)
}

Pointers and Structs

type Point struct {
  x int
  y int
}

func main() {
  pt := Point{8, 1}
  complement(&pt)
  fmt.Printf("Result= %d and %d\n", pt.x , pt.y)
}

func complement(p *Point) {
  p.x, p.y = -p.y, -p.x
}

Slices


func main() {
    s := make([]string, 3)
    fmt.Println("emp:", s) //emp: [  ]

    s[0] = "a"
    s[1] = "b"
    s[2] = "c"
    fmt.Println("set:", s) //set: [a b c]
    fmt.Println("get:", s[2]) //get: c

    fmt.Println("len:", len(s)) //len: 3

    s = append(s, "d")
    s = append(s, "e", "f")
    fmt.Println("apd:", s) //apd: [a b c d e f]

    c := make([]string, len(s))
    copy(c, s)
    fmt.Println("cpy:", c) //cpy: [a b c d e f]

    l := s[2:5]
    fmt.Println("sl1:", l) //sl1: [c d e]

    l = s[:5]
    fmt.Println("sl2:", l) //sl2: [a b c d e]

    l = s[2:]
    fmt.Println("sl3:", l) //sl3: [c d e f]

    t := []string{"g", "h", "i"}
    fmt.Println("dcl:", t) //dcl: [g h i]

    twoD := make([][]int, 3)
    for i := 0; i < 3; i++ {
        innerLen := i + 1
        twoD[i] = make([]int, innerLen)
        for j := 0; j < innerLen; j++ {
            twoD[i][j] = i + j
        }
    }
    fmt.Println("2d: ", twoD) //2d:  [[0] [1 2] [2 3 4]]
}