by communicating. • The bigger the interface, the weaker the abstraction. • Gofmt's style is no one's favorite, yet gofmt is everyone's favorite. • A little copying is better than a little dependency. • Clear is better than clever. • Reflection is never clear. • etc..
+ 1 const ( _ = iota // ignore first value by assigning to blank identifier KB uint64 = 1 << (10 * iota) MB GB ) // If you use iota + 1 for above example, don't forget parenthesis of "iota + 1". // ⭕ 1 << (10 * (iota + 1)) // ❌ 1 << (10 * iota + 1) // Available for constants to assign other package constants. const Day = 24 * time.Hour // time.Hour is constant. // ❌ var Day = 24 * time.Hour
:= []struct { src, expected string }{ {"foo", "FOO"}, {"bar", "BAR"}, } for _, c := range candidates { actual := strings.ToUpper(c.src) if c.expected != actual { // should be good error messages. t.Fatalf("Expected %v, but got %v\n", c.expected, actual) } } }
i := 0; i < b.N; i++ { re := regexp.MustCompile("[a-z]{3}") // ❌ _ = re.FindAllString(`Lorem ipsum dolor sit amet, consectetur..`) } } func BenchmarkRegexpMustCompile2(b *testing.B) { re := regexp.MustCompile("[a-z]{3}") // ⭕ b.ResetTimer() for i := 0; i < b.N; i++ { _ = re.FindAllString(`Lorem ipsum dolor sit amet, consectetur..`) } }
concurrent goroutines • Using a sync.Mutex to protect a cache • Causes degraded performance using the same Regexp • Copy method is available for go1.6 or later
mind to write go code. • https://go-proverbs.github.io • Understand how to handle Constants. • Parallelism Testing and Benchmarking. • Not to put regexp on your code as possible. • Use strings package instead of it. • (And) Read Release Notes • https://golang.org/doc/go1.7