These are the Go language-specific coding styles. Anything mentions here overrides/adds to the "In General" guides.
The Go communities and development team had spend almost all their strength to develop such a powerful linter checking. Use them before committing the codes.
/*
Package regexp implements a simple library for regular expressions.
The syntax of the regular expressions accepted is:
regexp:
concatenation { '|' concatenation }
concatenation:
{ closure }
closure:
term [ '*' | '+' | '?' ]
term:
'^'
'$'
'.'
character
'[' [ '^' ] character-ranges ']'
'(' regexp ')'
*/
package regexp
error
approachpanic without encouraging recovery()
approachlog.Printf("Reading %s: %v", filename, err)
.CamelCase
convention.camelCase
for private functions, big CamelCase
for public functions and APIThese are the proper Go practices:
// Declare but not initialized - use var
var players int // 0
var things []Thing // an empty slice of Things
var thing Thing // empty Thing struct
// Declare and initialized - use :=
min, max := 0, 1000
things := nil
thing := &Thing{}
// ambiguous types, use either but clarify up with function or type
var length uint32 = 0x80
length := uint32(0x80)
func main() {
r, err := Open("a")
if err != nil {
log.Fatalf("error opening 'a'\n")
}
defer func() {
err := r.Close()
if err != nil {
log.Fatal(err)
}
}()
r, err = Open("b")
if err != nil {
log.Fatalf("error opening 'b'\n")
}
defer func() {
err := r.Close()
if err != nil {
log.Fatal(err)
}
}()
}
That's all about Go coding styles conventions.