Go fmt 为数学表达式生成格式不一致的结果

Go fmt producing inconsistently formatted results for math expressions

我的理解是 go fmt 应该生成可读且格式一致的代码。但是我发现情况并非如此。

我输入了注释代码,然后 fmt return编辑了未注释的代码。为什么它崩溃了 0.5*(y3-y0),而不是 0.5 * (y2 - y0)?这怎么一致?而且,IMO,几乎每个 space 都崩溃的 return 行是可读性灾难。

不一致是一个错误吗? 有没有办法让 go fmt 单独留下一些行(比如 return 行)?

func cubicInterpolate(x, y0, y1, y2, y3 float64) float64 {
    // 4-point, 3rd-order Hermite (x-form)
    // c0 := y1
    // c1 := 0.5 * (y2 - y0)
    // c2 := y0 - 2.5 * y1 + 2. * y2 - 0.5 * y3
    // c3 := 1.5 * (y1 - y2) + 0.5 * (y3 - y0)
    //
    // return ((c3 * x + c2) * x + c1) * x + c0

    c0 := y1
    c1 := 0.5 * (y2 - y0)
    c2 := y0 - 2.5*y1 + 2.*y2 - 0.5*y3
    c3 := 1.5*(y1-y2) + 0.5*(y3-y0)

    return ((c3*x+c2)*x+c1)*x + c0
}

这在 go source code 中有记录;目的是明确运算符的优先级。例如,在您的示例中 y0 - 2.5*y1 + 2.*y2 - 0.5*y3 乘法将在减法之前执行,并且所选格式一目了然。

回答你的问题;这不是错误;事实上 quite a bit of effort was put into the formatting. You cannot exclude a line from formatting; this is by design, as mentioned in the FAQ,目的是执行 layont 规则:

gofmt is a pretty-printer whose purpose is to enforce layout rules; it replaces the usual compendium of do's and don'ts that allows interpretation.

下面是来自 go/printer/nodes.go 的格式化摘录,详细说明了格式规则:

Format the binary expression: decide the cutoff and then format. Let's call depth == 1 Normal mode, and depth > 1 Compact mode. (Algorithm suggestion by Russ Cox.)

The precedences are: 5 * / % << >> & &^ 4 + - | ^ 3 == != < <= > >= 2 && 1 ||

The only decision is whether there will be spaces around levels 4 and 5. There are never spaces at level 6 (unary), and always spaces at levels 3 and below.

To choose the cutoff, look at the whole expression but excluding primary expressions (function calls, parenthesized exprs), and apply these rules:

  1. If there is a binary operator with a right side unary operand that would clash without a space, the cutoff must be (in order):
   /* 6   
   && 6   
   &^ 6   
   ++ 5   
   -- 5 

(Comparison operators always have spaces around them.)

  1. If there is a mix of level 5 and level 4 operators, then the cutoff is 5 (use spaces to distinguish precedence) in Normal mode and 4 (never use spaces) in Compact mode.

  2. If there are no level 4 operators or no level 5 operators, then the cutoff is 6 (always use spaces) in Normal mode and 4 (never use spaces) in Compact mode.