The Go model of code formatting conventions is "gofmt
is the convention". There is one part of that convention I'm having difficulty in understanding, and it would be great to have a formal definition of which gofmt
is an implementation, rather than having to deduce the model from empirical examples. Here's a sample.
Before go fmt
:
func sieve(mine int, // This instance's own prime
inch chan int, // Input channel from lower primes
done chan int, // Channel for signalling shutdown
count int) { // Number of primes - counter
start := true // First-number switch
ouch := make(chan int) // Output channel, this instance
fmt.Printf("%v ", mine) // Print this instance's prime
After go fmt
:
func sieve(mine int, // This instance's own prime
inch chan int, // Input channel from lower primes
done chan int, // Channel for signalling shutdown
count int) { // Number of primes - counter
start := true // First-number switch
ouch := make(chan int) // Output channel, this instance
fmt.Printf("%v ", mine) // Print this instance's prime
Can anyone help me understand what's going on here? That is, why have some comments been detrimentally compressed, and others expanded? Some theories:
gofmt
Conventionally, arguments are described in the function/method documentation. Consider math/big.(*Int).Exp
docs:
// Exp sets z = x**y mod |m| (i.e. the sign of m is ignored), and returns z.
// If y <= 0, the result is 1 mod |m|; if m == nil or m == 0, z = x**y.
// See Knuth, volume 2, section 4.6.3.
func (z *Int) Exp(x, y, m *Int) *Int {
The main comment explains what x
, y
and m
are and the relationships between them. This is how it looks rendered by godoc.
Same for code, each line usually has its own comment line:
// First-number switch.
start := true
// Output channel, this instance.
ouch := make(chan int)
// Print this instance's prime.
fmt.Printf("%v ", mine)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With