Go, as the machine guides
Emmanuel T Odeke
Orijtech, Inc.
Thu 2 Dec 2021
Emmanuel T Odeke
Orijtech, Inc.
Thu 2 Dec 2021

// SanitizeGenesisBalances sorts addresses and coin sets. func SanitizeGenesisBalances(balances []Balance) []Balance { sort.Slice(balances, func(i, j int) bool { addr1, _ := sdk.AccAddressFromBech32(balances[i].Address) addr2, _ := sdk.AccAddressFromBech32(balances[j].Address) return bytes.Compare(addr1.Bytes(), addr2.Bytes()) < 0 }) for _, balance := range balances { balance.Coins = balance.Coins.Sort() } return balances }
sdk.AccAddressFromBech32 returns an error that's ignored?// SanitizeGenesisBalances sorts addresses and coin sets. func SanitizeGenesisBalances(balances []Balance) []Balance { sort.Slice(balances, func(i, j int) bool { addr1, _ := sdk.AccAddressFromBech32(balances[i].Address) addr2, _ := sdk.AccAddressFromBech32(balances[j].Address) return bytes.Compare(addr1.Bytes(), addr2.Bytes()) < 0 }) for _, balance := range balances { balance.Coins = balance.Coins.Sort() } return balances }


package main func main() { ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, %s", r.Proto) })) ts.EnableHTTP2 = true ts.StartTLS() defer ts.Close() res, err := ts.Client().Get(ts.URL) if err != nil { log.Fatal(err) } greeting, err := ioutil.ReadAll(res.Body) res.Body.Close() if err != nil { panic(err) } println(string(greeting)) }
GODEBUG=http2debug=2 and sent 10s of thousands of requests then tallied up the failures and discovered oddities{"contentType": null} caused the new production C++ backend to crash, it hadn't been properly fuzzed nor tested; a static analyzer could have caught thispackage main func main() { m := map[string]int{"ten": 10} ten := []byte("ten") // Expensive way. key := string(ten) println(m[key]) // Cheap way. println(m[string(ten)]) }
delete(m, key)package main func clearNonFast(m map[string]int) { keys := make([]string, 0, len(m)) for key := range m { keys = append(keys, key) } // Do something with keys. // _ = keys for _, key := range keys { delete(m, key) } } func clearFast(m map[string]int) { for key := range m { delete(m, key) } }

package main import ( "fmt" "reflect" "unsafe" ) func unsafeByteSliceToStr(b []byte) string { return *(*string)(unsafe.Pointer(&b)) } func unsafeStrToByteSlice(s string) (b []byte) { hdr := (*reflect.SliceHeader)(unsafe.Pointer(&b)) hdr.Cap = len(s) hdr.Len = len(s) hdr.Data = (*reflect.StringHeader)(unsafe.Pointer(&s)).Data return b } func main() { fmt.Printf("%q\n", unsafeByteSliceToStr([]byte("string"))) fmt.Printf("% x\n", unsafeStrToByteSlice("string")) }
Available at americanexpress/simplemli/pull/4

fmt.Printf("This is my name: %s\n", string(nameInByteSlice))fmt.Printf("This is my name: %s\n", nameInByteSlice)



go test or add staticcheck to your pipelines to run static analyzers
runtime/sys_darwing.go, when making a syscall, there was a typo to use entersyscallblock() instead of entersyscallGODEBUG=scheddetail=1,schedtrace=X ./go_binary where X is a value in milliseconds-S when building your Go binariespackage main func main() { println("An honor to be at American Express.. Don't live life without it!") }

go tool trace file to introspect the state of the Go schedulerEmmanuel T Odeke
Orijtech, Inc.
Thu 2 Dec 2021