You are on page 1of 1

Functions

cheat sheet func add(a int, b int) float64 {


return float64(a + b) }
Installing Go Arrays func tuple() (int, int) {
var a [5]int // fixed size return 4, 2 }
https://golang.org/doc/install x, y := tuple()
a[0] = 3 // assignment
$ go version func fvar(nums ...int) {/**/}
a := [...]int{1,3:2,3,6:-1}
Go program var a [2][3]int
pa := *[32]byte{}
Closures & Lambdas
package main func adder() func(int) int {
import "fmt" Slices sum := 0
import r "math/rand" return func(x int) int {
var s []int // dynamic size
sum += x
s := []int {1,2,3}
func main() { return sum } }
s := []byte("Hello")
fmt.Println("Hello",r.Int()) myLambda := func() bool {/**/}
s := make([]string, 3)
}
s = append(s, "d", "e") Defer
c := make([]string, len(s))
Build & Run copy(dst, src)
file, err := os.Create("foo")
$ ### RUN ### if err != nil {
x := s[2:5] // elem. 2,3,4
$ go run . return err
y := s[:5] // elem. < 5
$ ### VET & BUILD & RUN ### }
$ go vet hello.go Maps defer func() { file.Close() }()
$
$
go build hello.go
./hello
m := make(map[string]int) Structs & Methods
m["key1"] = 42
$ ### INSTALL & RUN ### type Person struct {
fmt.Println("map: ", m)
$ go install hello.go name string
m := map[string]int{"foo": 1,
$ $GOBIN/hello age int
"bar": 2} // initialize
}
Variables & Constants v := m["key1"]
_, contains := m["key2"]
func (p *Person) Aging(y int) {
p.age = p.age + y
// declaration length := len(m)
}
var msg string delete(m, "key1")
p := Person{name: "Bob", age: 4}
msg = "Hello"
p.age = 30
// short with type inference Loops p.Aging(1)
msg := "Hello"
for i := 0; i < 10; i++ {/**/}
// constants
for i <= 3 { i = i + 1 } Interfaces
const Pi = 3.14159
for {/**/ continue /**/ break} type geometry interface {
ip, port := "127.0.0.1", "8080"
area() float64
fmt.Println(ip + ":" + port)
Ranges perim() float64
Types s := []string{"a", "b", "c"} }
for idx, val := range s {/**/} func (r rect) area() float64 {}
str := "Hello" // string
m := map[string]int{"a": 1} func (r rect) perim() float64 {}
str := `Multiline string`
for k, v := range m {/**/} func measure(g geometry) {}
num := 3 // int
r := rect{width: 2, height: 4}
num := 3. // float64
num := 3 + 4i // complex128
Conditionals measure(r)
var c rune = '♬' // UTF-8 if d == "Sun" || d == "Sat" { Concurrency
num := byte('A') // byte/uint8 } else if d == "Mon" && foo() {
fmt.Printf("%T\n", i) // print } else if _, err := f(); func f(c chan int) {}
s := reflect.TypeOf(i).String() err != nil { c := make(chan int)
type Weight float64 // custom } else {/**/} cb := make(chan int, bufferLen)
w := Weight(70) // conversion go func() {
Switches fmt.Println(<-c)
Pointers switch time.Now().Weekday() {
}()
c <- 2 // send 2 to c
var pi *int = &i // point to i case 0:
x, y := <-c, <-c // recv from c
p := &i // point to i fallthrough
close(c) // close chan
*p = *p * 2 // dereferencing case 1: fmt.Println("Weekend")
select { case c <- x: /**/
ps.x == (*ps).x // equivalent default: fmt.Println("Workday")
Published 7th November 2020

case <-quit: return }


}

golang.sk Resources
Sync
var mu sync.Mutex // sync.Once
challenge | reshape | boost https://tour.golang.org/
https://github.com/golang/go/wiki // .Lock();.Unlock();once.Do(f)
hello@golang.sk https://repl.it/languages/go var wg sync.WaitGroup
https://www.golang.sk https://awesome-go.com/ // .Add(int);.Done();.Wait()

You might also like