Golang:带注释的代码行的惯例是什么?

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
  • 不完整(以某种方式)线条与完整线条的处理方式不同
  • 别的什么?

Ain*_*r-G 6

通常,参数在函数/方法文档中描述.考虑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,ym有和它们之间的关系.是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)

  • 不能在那里争论.FWIW,我不认为不鼓励使用相同的行注释,我只是认为你在单独的行中描述函数args的情况没有被优化.相同的行注释看起来不错:https://play.golang.org/p/JNpGqSD9Dz (2认同)