Last active
March 27, 2024 16:24
-
-
Save fillest/1de2d1dc694a814e0fba8a3ba7af4539 to your computer and use it in GitHub Desktop.
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
func PanicOn(err error) { | |
if err != nil { | |
panic(err) | |
} | |
} | |
multilineStr := `line1 | |
line2` | |
//tmp avoid "unused" | |
_ = i | |
//alt: func UNUSED(x ...interface{}) {} | |
////output | |
import "log" | |
log.SetFlags(log.LstdFlags | log.Lshortfile) | |
log.Print(a, b, c) | |
log.Printf(" %+v %d %s", ) //https://golang.org/pkg/fmt/#hdr-Printing | |
log.Fatalf("error %+v", ) | |
////reflection dump | |
import "github.com/davecgh/go-spew/spew" | |
spew.Config.DisableMethods = true | |
spew.Dump(var1, var2, ...) | |
log.Printf("... %s", spew.Sdump(err, ...)) | |
import "fmt" | |
panic(fmt.Errorf("unexpected type '%T'", smth)) | |
////iteration | |
//warn: val is a copy! also https://github.com/golang/go/wiki/CommonMistakes | |
for i, val := range aSlice {} | |
for _, val := range aSlice {} | |
for i := range aSlice {} | |
for key, val := range aMap {} | |
////do N times | |
for i := 1; i <= N; i++ {} | |
////sleep | |
import "time" | |
time.Sleep(100 * time.Millisecond) | |
////arrays, slices | |
//slice is a "ref" type (gets passed as ptr; nil by default) | |
//array is not (passing/assigning copies) | |
/*array:*/ [n]Type, [n]Type{v1, v2}, [...]Type{v1, v2} //'...' means length inferred | |
/*slice:*/ make([]Type, n), []Type{}, []Type{v1, v2} | |
//"dynamic array" | |
var arr []Type //nil (slice) | |
arr = append(arr, val) | |
vals := []int{1, 2, 3, 4, 5} | |
vals[:3] //[1 2 3] | |
vals[:10] //panic: runtime error: slice bounds out of range [:10] with capacity 5 | |
vals[3:] //[4 5] | |
vals[10:] //panic: runtime error: slice bounds out of range [10:5] | |
////if arr/map is empty | |
if len(arr) == 0 {} | |
////maps | |
//map is a "ref" type (gets passed as ptr; nil by default) | |
var m map[string]int //== nil; >like an empty map when reading; a write causes panic | |
m := map[string]int{} //or m := make(map[string]int) | |
//to clear just assign new empty value | |
v := m["k"] //value type's zero value if key doesn't exist | |
v, isInMap := m["k"] //value type's zero value if key doesn't exist | |
delete(m, "k") //>will do nothing if the specified key doesn't exist. | |
//delete() while iterating is ok https://github.com/golang/go/issues/9926 | |
////sets | |
set := make(map[int]struct{}) //empty struct has zero width | |
set[123] = struct{}{} | |
a := new(int) //can't `&int`. `new` is basically needed only for this (https://softwareengineering.stackexchange.com/a/216582) | |
////env vars | |
import "os" | |
s := os.Getenv("name") //"" if unset | |
s, isSet := os.LookupEnv("name") | |
////int <-> str | |
import "strconv" | |
strconv.Itoa(123) // + " str" | |
/*int64*/ strconv.FormatUint(ts, 10); strconv.FormatInt(ts, 10) | |
num, _ := strconv.Atoi("123") | |
var s strings.Builder | |
s.WriteString("...") | |
s.String() | |
////type cast | |
v1 := v.(string) //panics | |
v1, ok := v.(string) //if not ok, v will be the zero value of type //if v1, ok := v.(string); ok {} | |
switch v1 := v.(type) { | |
case int: | |
default: | |
panic(fmt.Errorf("unexpected type '%T'", v)) | |
} | |
////do with timeout | |
import "time" | |
done := make(chan bool, 1) | |
go func() { | |
defer helpers.CatchGoroPanic() | |
... | |
done <- true | |
}() | |
select { | |
case <-done: | |
case <-time.After(5*time.Second): | |
...timeout... | |
} | |
////latency-aware sleep-loop | |
import "time" | |
for { | |
startMoment := time.Now() | |
... | |
time.Sleep(1 * time.Second - time.Since(startMoment)) //>A negative or zero duration causes Sleep to return immediately | |
} | |
if strings.HasSuffix(s, "...") {} | |
workingDir, err := os.Getwd() | |
PanicOn(err) | |
import "math/rand" | |
rand.Seed(time.Now().UnixNano()) | |
time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond) | |
/usr/local/go/bin/go list -f '{{.Standard}} {{.ImportPath}}' ... | grep -e '^false' | |
cd ~/go/src/github.com/rakyll/hey/ && git log -n 3 && cd - |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment