Skip to content

gofmt formats the + operator inconsistently depending on context #1206

@mark-summerfield

Description

@mark-summerfield
Here is a tiny Go program:

package main

import "fmt"

func main() {
    var i, j int
    s := "word"
    i = j + 1
    if s[i:j + 1] == "no" {
        fmt.Println("oops")
    }
    m := make(map[string]int)
    m["a" + "b"] = 5
    s = "a" + "b"
}

Notice the two "j + 1" and two "a" + "b" expressions. Here
is what gofmt test.go produces:

package main

import "fmt"

func main() {
    var i, j int
    s := "word"
    i = j + 1
    if s[i:j+1] == "no" {
        fmt.Println("oops")
    }
    m := make(map[string]int)
    m["a"+"b"] = 5
    s = "a" + "b"
}

It seems that when the + operator is used inside square brackets surrounding whitespace
is removed; but in other contexts the surrounding whitespace is left alone. Personally I
wish it was left alone, but if you want to force consistency then surely it ought to be
one way or the other (and my preference is to have a space either side of binary
operators).

Which compiler are you using (5g, 6g, 8g, gccgo)?
8g

Which operating system are you using?
Ubuntu 32-bit

Which revision are you using?  (hg identify)
70ed048caad2+ tip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions