It's been said that naming is one of the two hardest problems in computer science along with cache invalidation and 'off-by-one' errors. (See what I did there?) Do you ever find yourself wondering what policies and practices you could adopt to make your life easier when reading code you wrote months ago? Or maybe you're up at night wishing you know how to write code in such a way as to maximize adoption and convenience for your users? Well, look no further because we've anticipated the need, solved the problem, and now we're sharing our knowledge and wisdom at no charge, all out of the goodness of our hearts in this comprehensive, totally no-nonsense (nudge, nudge, wink, wink) style guide of Go naming conventions.
What you are about to read might actually be helpful at some point but we're not betting on it. Don't try this at home...actually, do try this at home--but maybe don't try it at work.
package main
import "fmt"
func main() {
Message := "Always export local variable names"
fmt.Println(Message)
}
https://go.dev/play/p/8WVCvJpoa59
package main
import "fmt"
func main() {
const Message = "Always export constants defined in functions"
fmt.Println(Message)
}
https://go.dev/play/p/-0yZhHVNOOs
package main
import "fmt"
func main() {
Print("Always export input argument names")
}
func Print(Message string) {
fmt.Println(Message)
}
https://go.dev/play/p/utRBMOMQNgj
package main
import "fmt"
func main() {
Print("Always export output argument names")
}
func Print(Message string) (N int, Err error) {
return fmt.Println(Message)
}
https://go.dev/play/p/n5cJhLDKNWk
package main
import "fmt"
func main() {
new(Printer).Print("Always export receiver names")
}
type Printer struct{}
func (Printer *Printer) Print(Message string) (N int, Err error) {
return fmt.Println(Message)
}
https://go.dev/play/p/jEN-zkrjxdT
package main
import "fmt"
func main() {
new(Printer).Print(
"Use only the first letter of a type as the receiver for its methods (oh, wait...), " +
"and (per tip #5) make sure the receiver is exported")
}
type Printer struct{}
func (P *Printer) Print(Message string) (N int, Err error) {
return fmt.Println(Message)
}
https://go.dev/play/p/0OqQLnPPcVd
package main
import "fmt"
func main() {
new(Printer).Print("Use single-letter variables whenever possible")
}
type Printer struct{}
func (P *Printer) Print(M string) (N int, E error) {
return fmt.Println(M)
}
https://go.dev/play/p/Q1jgH_6h2kT
package main
import "fmt"
func main() {
new(Printer).Print("Use double-letter variables when you run out of single-letter variables")
}
type Printer struct{}
func (P *Printer) Print(NN string) (N int, E error) {
return fmt.Println(NN)
}
https://go.dev/play/p/k3p9Hf49-20
package main
import "fmt"
func main() {
new(Printer).Print("On second thought, use a generic receiver name like 'this', 'self', or 'me'.")
}
type Printer struct{}
func (this *Printer) Print(NN string) (N int, E error) {
return fmt.Println(NN)
}
https://go.dev/play/p/mSMZRqUy4qw
package main
import "fmt"
func main() {
new(Printer).Print("See what I did here? ;)")
}
type Printer struct{}
func (𝕥𝕙𝕚𝕤 *Printer) Print(NN string) (N int, E error) {
return fmt.Println(NN)
}
https://go.dev/play/p/VPpSDOZYYjT
package main
import fmt "fmt"
func main() {
fmt.Println("Always define import aliases")
}
https://go.dev/play/p/zCOnEoNtAf4
package main
import Fmt "fmt"
func main() {
Fmt.Println("Always export all imports")
}
https://go.dev/play/p/_fEPiypASub
package main
import F "fmt"
func main() {
F.Println("Use single-letter (exported) import aliases")
}
https://go.dev/play/p/e8JQAlSKpnZ
package main
import (
F "flag"
FF "fmt"
)
func main() {
F.Parse()
FF.Println("Use double-letter alias names when necessary")
}