This is a simple collection of my Go Playground snippets for reference later.
Last active
December 6, 2019 20:45
-
-
Save nickpoorman/c2f6701c07c8e7be54d1fbbc287c5b6b to your computer and use it in GitHub Desktop.
A Collection of Go Playground Snippets
This file contains hidden or 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
// https://play.golang.org/p/fq4hoHvxlV8 | |
package main | |
import ( | |
"crypto/sha1" | |
"fmt" | |
) | |
type ByteSliceMap struct { | |
m map[string]string | |
} | |
// Key generates a key for the map | |
func (b *ByteSliceMap) key(buf []byte) string { | |
h := sha1.New() | |
h.Write(buf) | |
sum := h.Sum(nil) | |
return fmt.Sprintf("%x", sum) | |
} | |
func (t *ByteSliceMap) Get(key []byte) (value string, ok bool) { | |
value, ok = t.m[t.key(key)] | |
return | |
} | |
func (t *ByteSliceMap) Put(key []byte, value string) { | |
if t.m == nil { | |
t.m = make(map[string]string) | |
} | |
t.m[t.key(key)] = value | |
} | |
func check(b *ByteSliceMap, k []byte) { | |
if v, ok := b.Get(k); ok { | |
fmt.Println(k, ",", v) | |
} else { | |
fmt.Println(k, "not found") | |
} | |
} | |
func main() { | |
m := &ByteSliceMap{} | |
k := []uint8{1, 2, 3} | |
v := "foo bar" | |
fmt.Println("adding", k, ",", v) | |
m.Put(k, v) | |
check(m, []byte{1, 2, 3}) | |
check(m, []byte{2, 3}) | |
k = []byte{2, 3} | |
v = "ping pong" | |
fmt.Println("adding", k, ",", v) | |
m.Put([]byte{2, 3}, v) | |
check(m, []byte{2, 3}) | |
// From a slice of bytes because "byte" is just an alias for uint8 | |
k = make([]byte, 6) | |
k[3] = 5 | |
v = "my string value to store" | |
fmt.Println("adding", k, ",", v) | |
m.Put(k, v) | |
check(m, []byte{0, 0, 0, 5, 0, 0}) | |
} |
This file contains hidden or 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" | |
"time" | |
"github.com/google/go-cmp/cmp" | |
) | |
type Foo struct { | |
Timestamp time.Time | |
Ping string | |
} | |
func ignoreKey(key string) cmp.Option { | |
opt := cmp.Comparer(func(x, y time.Time) bool { | |
return true | |
}) | |
fo := cmp.FilterPath(func(path cmp.Path) bool { | |
switch t := path.Last().(type) { | |
case cmp.StructField: | |
fmt.Printf("path: %#v\n", t.Name()) | |
if t.Name() == key { | |
return true | |
} | |
default: | |
return false | |
} | |
return false | |
}, opt) | |
return fo | |
} | |
func main() { | |
loc, _ := time.LoadLocation("America/New_York") | |
a := Foo{Timestamp: time.Date(2018, 8, 30, 12, 0, 0, 0, time.UTC), Ping: "pong"} | |
b := Foo{Timestamp: time.Date(2018, 8, 30, 12, 0, 0, 0, loc), Ping: "pong"} | |
fmt.Println("a: ", a) | |
fmt.Println("b: ", b) | |
fo := ignoreKey("Timestamp") | |
fmt.Println(cmp.Equal(a, b, fo)) | |
} | |
// a: {2018-08-30 12:00:00 +0000 UTC pong} | |
// b: {2018-08-30 12:00:00 -0400 EDT pon} | |
// path: "Timestamp" | |
// path: "Ping" | |
// true |
This file contains hidden or 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
// https://play.golang.org/p/-SrS8b_FcQJ | |
package main | |
import ( | |
"fmt" | |
) | |
type Int struct { | |
v int | |
} | |
func (i *Int) Release1() { | |
fmt.Printf("Release1: %v\n", i.v) | |
} | |
func (i *Int) Release2() { | |
fmt.Printf("Release2: %v\n", i.v) | |
} | |
func (i *Int) Release3() { | |
fmt.Printf("Release3: %v\n", i.v) | |
} | |
func test() { | |
ints := make([]Int, 0, 10) | |
for i := 10; i < 20; i++ { | |
ints = append(ints, Int{v: i}) | |
} | |
defer func() { | |
for _, in := range ints { | |
in.Release1() | |
} | |
}() | |
for i := range ints { | |
defer ints[i].Release2() | |
} | |
for _, in := range ints { | |
defer in.Release3() | |
} | |
fmt.Printf("ints: %v\n", ints) | |
} | |
func main() { | |
test() | |
} | |
// ints: [{10} {11} {12} {13} {14} {15} {16} {17} {18} {19}] | |
// Release3: 19 | |
// Release3: 19 | |
// Release3: 19 | |
// Release3: 19 | |
// Release3: 19 | |
// Release3: 19 | |
// Release3: 19 | |
// Release3: 19 | |
// Release3: 19 | |
// Release3: 19 | |
// Release2: 19 | |
// Release2: 18 | |
// Release2: 17 | |
// Release2: 16 | |
// Release2: 15 | |
// Release2: 14 | |
// Release2: 13 | |
// Release2: 12 | |
// Release2: 11 | |
// Release2: 10 | |
// Release1: 10 | |
// Release1: 11 | |
// Release1: 12 | |
// Release1: 13 | |
// Release1: 14 | |
// Release1: 15 | |
// Release1: 16 | |
// Release1: 17 | |
// Release1: 18 | |
// Release1: 19 |
This file contains hidden or 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
// https://play.golang.org/p/leJomgRt7Zn | |
package main | |
import ( | |
"fmt" | |
) | |
type Dog struct { | |
Age int | |
Name string | |
} | |
func main() { | |
dog1 := Dog{5, "Roger"} | |
dog2 := dog1 | |
fmt.Printf("dog1: %v | dog2: %v\n", dog1.Age, dog2.Age) | |
if dog1 == dog2 { | |
fmt.Println("dog1 and mydog are equal structs") | |
} | |
fmt.Printf("dog1: %p | dog2: %p\n", &dog1, &dog2) | |
dog2.Age = 1 | |
fmt.Printf("dog1: %v | dog2: %v\n", dog1.Age, dog2.Age) | |
} | |
// dog1: 5 | dog2: 5 | |
// dog1 and mydog are equal structs | |
// dog1: 0x40a0e0 | dog2: 0x40a0f0 | |
// dog1: 5 | dog2: 1 |
This file contains hidden or 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
// https://play.golang.org/p/jSV0HKc7qNX | |
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
foo := make([]string, 0, 5) | |
foo = append(foo, "a") | |
foo = append(foo, "b") | |
foo = append(foo, "c") | |
fmt.Printf("len: %v | cap: %v\n", len(foo), cap(foo)) | |
bar := foo[:len(foo):len(foo)] | |
fmt.Printf("len: %v | cap: %v\n", len(bar), cap(bar)) | |
} | |
// len: 3 | cap: 5 | |
// len: 3 | cap: 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment