Use lowercase and short, meaningful names.
Avoid underscores (_) or mixed caps.
Use PascalCase .
Name structs explicitly and meaningfully.
type CustomerManager struct {
// Fields
}
Use PascalCase for exported functions.
Use camelCase for unexported functions.
func CalculateTotal () int {
return 0
}
func processOrder () {
// Internal function
}
1.4 Variable and Field Naming
Use camelCase for local variables.
Use PascalCase for exported struct fields.
Use short, clear, and meaningful names.
var customerName string
// Inside a struct
type Customer struct {
Name string // Exported field
email string // Unexported field
}
Use PascalCase for exported constants.
Use camelCase for unexported constants.
const MaxRetryCount = 5
const apiKey = "12345"
Use -er suffix when possible.
type Reader interface {
Read (p []byte ) (n int , err error )
}
type CustomerRepository interface {
Save (customer Customer ) error
}
Define errors using errors.New.
Use Err prefix.
var ErrNotFound = errors .New ("not found" )
2. Comments and Documentation
2.1 Using GoDoc Comments
Use // for single-line comments.
Use full sentences starting with the function name.
// CustomerManager handles customer operations.
type CustomerManager struct {}
// AddCustomer adds a new customer.
func (cm * CustomerManager ) AddCustomer (c Customer ) {
// Implementation
}
3.1 Using Testing Package
Use Go's built-in testing package.
Name test functions as Test[Function].
Use *_test.go suffix for test files.
package customer_test
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestAddCustomer (t * testing.T ) {
cm := customer.CustomerManager {}
c := customer.Customer {Name : "John Doe" }
err := cm .AddCustomer (c )
assert .NoError (t , err )
}