Bre*_*ugh 6 code-formatting go
代码格式约定转到模型" gofmt 是惯例".该惯例的一部分我很难理解,如果有一个正式的定义gofmt是一个实现,而不是必须从经验例子中推导出模型,那将是很好的.这是一个样本.
之前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
Run Code Online (Sandbox Code Playgroud)
之后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
Run Code Online (Sandbox Code Playgroud)
谁能帮我理解这里发生了什么?也就是说,为什么有些评论被不利地压缩,而其他评论则扩大了?一些理论:
gofmt通常,参数在函数/方法文档中描述.考虑math/big.(*Int).Exp文档:
// 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 {
Run Code Online (Sandbox Code Playgroud)
主要意见说明了什么x,y并m有和它们之间的关系.这是godoc渲染的外观.
对于代码来说,每行通常都有自己的注释行:
// First-number switch.
start := true
// Output channel, this instance.
ouch := make(chan int)
// Print this instance's prime.
fmt.Printf("%v ", mine)
Run Code Online (Sandbox Code Playgroud)