Last active
November 3, 2020 13:42
-
-
Save antalakas/f2dcaef33d00677fe62c57faa95cd910 to your computer and use it in GitHub Desktop.
Go Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
i := 1 | |
for i <= 3 { | |
fmt.Println(i) | |
i = i + 1 | |
} | |
for j := 7; j <= 9; j++ { | |
fmt.Println(j) | |
} | |
for { | |
fmt.Println("loop") | |
break | |
} | |
for n := 0; n <= 5; n++ { | |
if n%2 == 0 { | |
continue | |
} | |
fmt.Println(n) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
go list ./... | grep -v /vendor/ | xargs -L1 golint | wc -l |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
m := make(map[string]int) | |
m["k1"] = 7 | |
m["k2"] = 13 | |
fmt.Println("map:", m) | |
v1 := m["k1"] | |
fmt.Println("v1: ", v1) | |
fmt.Println("len:", len(m)) | |
delete(m, "k2") | |
fmt.Println("map:", m) | |
_, prs := m["k2"] | |
fmt.Println("prs:", prs) | |
n := map[string]int{"foo": 1, "bar": 2} | |
fmt.Println("map:", n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nums := []int{2, 3, 4} | |
sum := 0 | |
for _, num := range nums { | |
sum += num | |
} | |
fmt.Println("sum:", sum) | |
for i, num := range nums { | |
if num == 3 { | |
fmt.Println("index:", i) | |
} | |
} | |
kvs := map[string]string{"a": "apple", "b": "banana"} | |
for k, v := range kvs { | |
fmt.Printf("%s -> %s\n", k, v) | |
} | |
for k := range kvs { | |
fmt.Println("key:", k) | |
} | |
for i, c := range "go" { | |
fmt.Println(i, c) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s := make([]string, 3) | |
fmt.Println("emp:", s) | |
s[0] = "a" | |
s[1] = "b" | |
s[2] = "c" | |
fmt.Println("set:", s) | |
fmt.Println("get:", s[2]) | |
fmt.Println("len:", len(s)) | |
s = append(s, "d") | |
s = append(s, "e", "f") | |
fmt.Println("apd:", s) | |
c := make([]string, len(s)) | |
copy(c, s) | |
fmt.Println("cpy:", c) | |
l := s[2:5] | |
fmt.Println("sl1:", l) | |
l = s[:5] | |
fmt.Println("sl2:", l) | |
l = s[2:] | |
fmt.Println("sl3:", l) | |
t := []string{"g", "h", "i"} | |
fmt.Println("dcl:", t) | |
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
type person struct { | |
name string | |
age int | |
} | |
func newPerson(name string) *person { | |
p := person{name: name} | |
p.age = 42 | |
return &p | |
} | |
func main() { | |
fmt.Println(person{"Bob", 20}) | |
fmt.Println(person{name: "Alice", age: 30}) | |
fmt.Println(person{name: "Fred"}) | |
fmt.Println(&person{name: "Ann", age: 40}) | |
fmt.Println(newPerson("Jon")) | |
s := person{name: "Sean", age: 50} | |
fmt.Println(s.name) | |
sp := &s | |
fmt.Println(sp.age) | |
sp.age = 51 | |
fmt.Println(sp.age) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment