-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGo-Cheat-Sheet.go
103 lines (81 loc) · 1.53 KB
/
Go-Cheat-Sheet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Go Cheat Sheet
package main
import (
"fmt"
"time"
)
func main() {
//primitive Data types
var x = "Hello World\n"
y := "Hello World\n"
fmt.Printf(x)
fmt.Printf(y)
var z = int(42)
w := int(42)
fmt.Printf("%d\n%d\n", z, w)
//Slices and Maps ugh
var mySlice = make([]string, 0)
mySlice = append(mySlice, "hek")
var myMap = make(map[string]string)
myMap["some key"] = "some string"
//Pointers
var count = int(42)
var ptr = &count
fmt.Println(ptr) //prints memory address of count
fmt.Println(*ptr) //prints the value of count
*ptr = 100 //writes new value into count
fmt.Println(count)
//Structs
var Person101 = new(Person)
Person101.Name = "Gary"
Person101.Age = 21
Person101.SayHello()
//Interfaces
var Person2 = new(Person)
Person2.Name = "Dweeb"
Greet(Person2)
//Control structures
if z == 1 {
fmt.Println("Z is equal to 1")
} else {
fmt.Println("Z is not equal to 1")
}
var hek = "foo"
//Switch statment
switch hek {
case "foo":
fmt.Println("found foo")
case "bar":
fmt.Println("Found bar")
default:
fmt.Println("Default case")
}
//For Loop
for i := 0; i < 10; i++ {
fmt.Println(i)
}
//Concurrency
go f()
time.Sleep(1 * time.Second)
fmt.Println("main function")
//end of main
}
//Person is a struct
type Person struct {
Name string
Age int
}
//SayHello to annoying go lint
func (p *Person) SayHello() {
//like dis
fmt.Println("hello", p.Name)
}
type Friend interface {
SayHello()
}
func Greet(f Friend) {
f.SayHello()
}
func f() {
fmt.Println("f function")
}