don’t know what programming language you know, I don’t know how good of a programmer you are, but there’s one thing I do know: the code you write will change.
of the package. - It should consist only of letters, short and clear. - It should reflect specific, well-defined responsibilities. - Must sense what package contains.
from the user - Avoid repetition (not http.HTTPServer) - Familiar abbreviation hurts nobody (strconv, fmt) - utils, helpers, common are bad names, no sense of what the package contains. - Imagine how you will import and call the function.
- i, j and k are good for for-loops - n for count, sum / amount, s for strings - k and v are good for maps - a, b, c can be used for comparing same types - x, y for local variable scope. variable
return fmt.Errorf("address field is required") } switch { case s.Port == 0: return fmt.Errorf("port field is required") case s.Port > 9999: return fmt.Errorf("invalid port number") } fmt.Printf("setting server address: %s:%d\n", s.Address, s.Port) if s.TLSEnabled { fmt.Println("TLS enabled") } return nil } type Server struct { Address string Port int TLSEnabled bool }
// type definition type myApiKey = apiKey // type alias func inspectString(s apiKey) { fmt.Println("s is", s) } func main() { var a apiKey = "api-key" var b myApiKey = "my-api-key" inspectString(a) // apiKey type inspectString(b) // myApiKey type inspectString("c") // string type } // s is api-key // s is my-api-key // s is c
from a function - Avoid init() as much as possible! - Instead of implementing custom concurrent safe map, use sync.Map - Use string concatenation instead of fmt.Print family - Use functions as types, especially in test cases *