Go cheatsheet -

Variables
Variable declaration
var msg string
msg = "Hello Go"
Shortcut of variable declaration
msg := "Hello Go"
Basic types
bool

string

int  int8  int16  int32  int64
uint uint8 uint16 uint32 uint64 uintptr

byte // alias for uint8

rune // alias for int32
     // Unicode code point

float32 float64

complex64 complex128
Numbers
num := 3          // int
num := 3.14       // float64
num := 3 + 4i     // complex128
num := byte('a')  // byte (alias for uint8)
var num float32 = 3.1415  // 32-bit float
var num uint = 5        // uint (unsigned)
Strings
var str1 string
str1 = "hello world"

str := "Single line String"

str := `Multiline
string`
Pointers
func main () {
  b := *getPointer()
  fmt.Println("The pointer value is", b)
}
 
func getPointer () (myPointer *int) {
  a := 1234
  return &a
}
Arrays
numbers := [...]int{0, 0, 0, 0}
Slice
slice := []int{2, 3, 4}
slice := []byte("HelloWorld")
Structs
// A struct is a collection of fields.

type Vertex struct {
    X int
    Y int
}

v := Vertex{1, 2}
v.X = 3
fmt.Println(v.X)
Struct Literals
var (
    v1 = Vertex{1, 2} // {1 2} 
    
    // Field names can be omitted
    v2 = Vertex{1, 2} // {1 2} 
    
    // Y is implicit
    v3 = Vertex{X: 1} //  {1 0} 
    
    v4 = Vertex{}    // {0 0}
    
    // & returns a pointer to the struct value
    p  = &Vertex{1, 2}  // &{1 2}
)
Maps
// A map maps keys to values.

package main

import "fmt"

type Vertex struct {
X, Y float64
}

var m map[string]Vertex

func main() {
m = make(map[string]Vertex)
m["addr1"] = Vertex{ 10, 20}
    m["addr2"] = Vertex{ 15, 25}
fmt.Println(m["addr1"])
    fmt.Println(m["addr2"])
}
Constants
const pi = 3.1415926
const app = "lautturi"
Control Structures
IF
if day == "monday" {
  fmt.Println("monday")
} else if day == "sunday" || day == "saturday" {
  fmt.Println("weekend")
} else {
  // otherwise
}
Switch
switch num {
  case 1:
    // don't "fall through" by default!
    fallthrough

  case 2:
    rest()

  default:
    work()
}
For-Range
arrName := []string{"tom","jack","bob"}
  for i, val := range arrName {
    fmt.Printf("idx %d, value: %s \n", i, val)
  }
For
for count := 0; count <= 10; count++ {
fmt.Println("couter:", count)
}
Functions
Basic function
// function add takes two parameters of type int
// and the return type is also int 
func add(x int, y int) int {
    return x + y
}
Anonymous function
// the function take zero arguments
// the return type is bool
myFunc := func() bool {
  return 5 < x && x < 10 
}
Multiple return types
a, b := getMsg()
func getMsg() (a string, b string) {
  return "Hello", "World"
}
Named return values
func circle(radius float64) (area, girth float64) {
  area = 3.1415*radius*radius
  girth = 2*3.1515*radius 
  return
}
Concurrency
goroutine
A goroutine is a lightweight thread.

package main
import (
  "fmt"
)
func say(s string) {
  fmt.Println(s)
}
func main() {
  go say("world") // managed by the Go runtime
  say("hello") // starts a new goroutine running
}
Channels
//create a channel
ch := make(chan int)

ch <- v  // Send v to channel ch.
v := <-ch //Receive value from ch.
Buffered channels
// the buffer length is 3
// ch := make(chan int, 3)

package main

import "fmt"

func main() {
  ch := make(chan int, 2)
  ch <- 1
  ch <- 2
  //ch <- 3
  fmt.Println(<-ch)
  fmt.Println(<-ch)
}
Closing channels
Sender:
close(ch) // close a channel

Receivers:
v, ok := <-ch // if ok is false, the channel is closed
for i := range ch // receive values  until the channel is closed
Packages
package main // Programs start running in the package named main.

import (  // import other packages
    "fmt"
    "math/rand"
)

func main() {
    fmt.Println("My favorite number is", rand.Intn(10))
}
Import packages
// multiple import statements
import "fmt"
import "math"

// factored import statement (groups)
import (
    "fmt"
    "math"
)
Package aliases
import r "math/rand"

r.Intn(10)
Exported names
// A name is exported if it begins with a capital letter.
func Myfun() {
  ...
}
func privfun(){
  ...
}

//----------------

import "mypkg"
// you can refer only to its exported names
mypkg.Myfun()
type Vertex struct {
X, Y float64
}

//  define methods on Vertex
func (v Vertex) Abs() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

v := Vertex{1, 2}
v.Abs() // 2.23606797749979
Interfaces
// An interface type is defined as a set of method signatures.
package main

import (
"fmt"
)

type Power interface {
Output() float32
}

func main() {
var p Power
b := Battery{"apple","p7"}
u := USB(1)

p = b  // p MyFloat implements Power
    fmt.Println(p.Output())
    
p = &u // p *Vertex implements Power
fmt.Println(p.Output())
}

type Battery struct {
    brand, modal string
}

func (b Battery) Output() float32 {
    if b.brand == "apple" {
        return 4.2
    } else {
        return 3.7
    }
}

type USB float32

func (u USB) Output() float32 {
    switch u {
      case 1:
        return 5.1
      case 2:
        return 4.7
      default:
        return 5.0
    }
}