go: cmd/gofmt: reformats block comments

by borman@google.com:

What steps will reproduce the problem?

Use /* */ for block comments.  Example:

func f() {
        /*
         * don't do this right now
        fmt.Printf(`a
multi-line
string`)
        */
        return
}

Gofmt alters the content of the comment (which includes the /* and */).  The first time
gofmt is called it produces:

func f() {
        /*
                 * don't do this right now
                fmt.Printf(`a
        multi-line
        string`)
        */
        return
}

and if called again it produces:

func f() {
        /*
                         * don't do this right now
                        fmt.Printf(`a
                multi-line
                string`)
        */
        return
}

gofmt should not have altered the comment in the first place.  The /* was indented
correctly up front.

About this issue

  • Original URL
  • State: open
  • Created 11 years ago
  • Reactions: 3
  • Comments: 29 (23 by maintainers)

Commits related to this issue

Most upvoted comments

It seems impossible, at least for me, to get the following reproducer test to pass; if you run gofmt before running go test. Because gofmt will always reformat the // output comment. And hence this sounds like a bug to me rather than a feature request.

package main

import (
	"fmt"
)

func Hello() {
	fmt.Printf("Hello \n\nWorld")
}

// 1. https://golang.org/pkg/testing/#hdr-Examples
// 2. https://blog.golang.org/examples
func ExampleHello() {
	Hello()

	// Output:
	// Hello 
	//
	// World
}

I have an Example test case:

func ExampleDecode() {
	call-my-test-case it print some output

	// Output:
	/*
here is the output in a block comment
   in multi lines with different indentation
	*/
}

The test case is passing, it’s no problem

but when I run go fmt, it constantly change the block comment to this style

	/*
	here is the output in a block comment
	   in multi lines with different indentation
	*/

and causing test case to FAIL;

wonder is it the same case here, is there any way to let go fmt not touching my block comment?

I just spent 1 hour trying to get gofmt to leave alone the // output: of a test example I was working on(https://github.com/golang/go/issues/41980), to no avail; until I searched and came across this issue.

I feel like gofmt re-formatting block comments in the specific case of // output: of a test example should be a separate issue that should be decided on independent of this one. But maybe this is just my frustration showing.

It is a real pain to work with multi-line // output: in example tests. I’m trying to assert YAML value.

  • /* ... */ gets broken by gofmt.
  • // per-line is unmanageable, I use Goland to comment multiple lines with hotkey, but it does not add space between // and line content.

Currently I have to paste expected content, add // with hotkey and then replace // to // to make things work.

I would appreciate that gofmt does not touch comments following // output:.