你正在使用的Go版本是什么( go version
)?
$ go version
go version go1.21.1 windows/amd64
这个问题在最新版本中是否会重现?
是的。
你正在使用什么操作系统和处理器架构( go env
)?
go env
输出
$ go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\icefl\AppData\Local\go-build
set GOENV=C:\Users\icefl\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\icefl\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\icefl\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:/Program Files/Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.21.1
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=D:\go-wargaming\go.mod
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\icefl\AppData\Local\Temp\go-build4212433334=/tmp/go-build -gno-record-gcc-switches
你做了什么?
我尝试格式化一些Go代码,一开始注解的格式化不正确。当我再次格式化时,产生了正确的结果。我没有最小示例,也无法创建一个更简单的示例,因此使用了字节数组。它来自我正在开发的代码生成器。code
package main
import (
"fmt"
"go/format"
)
var arr = []byte{47, 47, 32, 65, 117, 116, 111, 32, 103, 101, 110, 101, 114, 97, 116, 101, 100, 32, 102, 105, 108, 101, 33, 13, 10, 13, 10, 112, 97, 99, 107, 97, 103, 101, 32, 119, 97, 114, 103, 97, 109, 105, 110, 103, 13, 10, 13, 10, 105, 109, 112, 111, 114, 116, 32, 40, 13, 10, 9, 34, 99, 111, 110, 116, 101, 120, 116, 34, 13, 10, 9, 34, 103, 105, 116, 104, 117, 98, 46, 99, 111, 109, 47, 73, 99, 101, 102, 108, 111, 119, 82, 69, 47, 103, 111, 45, 119, 97, 114, 103, 97, 109, 105, 110, 103, 47, 118, 52, 47, 119, 97, 114, 103, 97, 109, 105, 110, 103, 47, 105, 110, 116, 101, 114, 110, 97, 108, 34, 13, 10, 9, 34, 103, 105, 116, 104, 117, 98, 46, 99, 111, 109, 47, 73, 99, 101, 102, 108, 111, 119, 82, 69, 47, 103, 111, 45, 119, 97, 114, 103, 97, 109, 105, 110, 103, 47, 118, 52, 47, 119, 97, 114, 103, 97, 109, 105, 110, 103, 47, 119, 103, 110, 34, 13, 10, 41, 13, 10, 13, 10, 47, 47, 32, 87, 103, 116, 118, 86, 101, 104, 105, 99, 108, 101, 115, 32, 114, 101, 116, 117, 114, 110, 115, 32, 108, 105, 115, 116, 32, 111, 102, 32, 118, 101, 104, 105, 99, 108, 101, 115, 32, 115, 111, 114, 116, 101, 100, 32, 98, 121, 32, 103, 97, 109, 101, 115, 32, 97, 110, 100, 32, 99, 111, 118, 101, 114, 101, 100, 32, 98, 121, 32, 118, 105, 100, 101, 111, 115, 46, 13, 10, 47, 47, 13, 10, 47, 47, 32, 104, 116, 116, 112, 115, 58, 47, 47, 100, 101, 118, 101, 108, 111, 112, 101, 114, 115, 46, 119, 97, 114, 103, 97, 109, 105, 110, 103, 46, 110, 101, 116, 47, 114, 101, 102, 101, 114, 101, 110, 99, 101, 47, 97, 108, 108, 47, 119, 103, 110, 47, 119, 103, 116, 118, 47, 118, 101, 104, 105, 99, 108, 101, 115, 13, 10, 47, 47, 13, 10, 47, 47, 32, 114, 101, 97, 108, 109, 58, 13, 10, 47, 47, 32, 32, 32, 32, 32, 86, 97, 108, 105, 100, 32, 114, 101, 97, 108, 109, 115, 58, 32, 82, 101, 97, 108, 109, 65, 115, 105, 97, 44, 32, 82, 101, 97, 108, 109, 69, 117, 44, 32, 82, 101, 97, 108, 109, 78, 97, 13, 10, 102, 117, 110, 99, 32, 40, 115, 101, 114, 118, 105, 99, 101, 32, 42, 87, 103, 110, 83, 101, 114, 118, 105, 99, 101, 41, 32, 87, 103, 116, 118, 86, 101, 104, 105, 99, 108, 101, 115, 40, 99, 116, 120, 32, 99, 111, 110, 116, 101, 120, 116, 46, 67, 111, 110, 116, 101, 120, 116, 44, 32, 114, 101, 97, 108, 109, 32, 82, 101, 97, 108, 109, 44, 32, 111, 112, 116, 105, 111, 110, 115, 32, 42, 119, 103, 110, 46, 87, 103, 116, 118, 86, 101, 104, 105, 99, 108, 101, 115, 79, 112, 116, 105, 111, 110, 115, 41, 32, 40, 42, 119, 103, 110, 46, 87, 103, 116, 118, 86, 101, 104, 105, 99, 108, 101, 115, 44, 32, 42, 71, 101, 110, 101, 114, 105, 99, 77, 101, 116, 97, 44, 32, 101, 114, 114, 111, 114, 41, 32, 123, 13, 10, 9, 105, 102, 32, 101, 114, 114, 32, 58, 61, 32, 118, 97, 108, 105, 100, 97, 116, 101, 82, 101, 97, 108, 109, 40, 114, 101, 97, 108, 109, 44, 32, 91, 93, 82, 101, 97, 108, 109, 123, 82, 101, 97, 108, 109, 65, 115, 105, 97, 44, 32, 82, 101, 97, 108, 109, 69, 117, 44, 32, 82, 101, 97, 108, 109, 78, 97, 125, 41, 59, 32, 101, 114, 114, 32, 33, 61, 32, 110, 105, 108, 32, 123, 13, 10, 9, 9, 114, 101, 116, 117, 114, 110, 32, 110, 105, 108, 44, 32, 110, 105, 108, 44, 32, 101, 114, 114, 13, 10, 9, 125, 13, 10, 13, 10, 9, 114, 101, 113, 80, 97, 114, 97, 109, 32, 58, 61, 32, 109, 97, 112, 91, 115, 116, 114, 105, 110, 103, 93, 115, 116, 114, 105, 110, 103, 123, 13, 10, 9, 125, 13, 10, 13, 10, 9, 105, 102, 32, 111, 112, 116, 105, 111, 110, 115, 32, 33, 61, 32, 110, 105, 108, 32, 123, 13, 10, 9, 9, 105, 102, 32, 111, 112, 116, 105, 111, 110, 115, 46, 67, 97, 116, 101, 103, 111, 114, 121, 73, 100, 32, 33, 61, 32, 110, 105, 108, 32, 123, 13, 10, 9, 9, 9, 114, 101, 113, 80, 97, 114, 97, 109, 91, 34, 99, 97, 116, 101, 103, 111, 114, 121, 95, 105, 100, 34, 93, 32, 61, 32, 105, 110, 116, 101, 114, 110, 97, 108, 46, 83, 108, 105, 99, 101, 73, 110, 116, 84, 111, 83, 116, 114, 105, 110, 103, 40, 111, 112, 116, 105, 111, 110, 115, 46, 67, 97, 116, 101, 103, 111, 114, 121, 73, 100, 44, 32, 34, 44, 34, 41, 13, 10, 9, 9, 125, 13, 10, 9, 9, 105, 102, 32, 111, 112, 116, 105, 111, 110, 115, 46, 80, 114, 111, 103, 114, 97, 109, 73, 100, 32, 33, 61, 32, 110, 105, 108, 32, 123, 13, 10, 9, 9, 9, 114, 101, 113, 80, 97, 114, 97, 109, 91, 34, 112, 114, 111, 103, 114, 97, 109, 95, 105, 100, 34, 93, 32, 61, 32, 105, 110, 116, 101, 114, 110, 97, 108, 46, 83, 108, 105, 99, 101, 73, 110, 116, 84, 111, 83, 116, 114, 105, 110, 103, 40, 111, 112, 116, 105, 111, 110, 115, 46, 80, 114, 111, 103, 114, 97, 109, 73, 100, 44, 32, 34, 44, 34, 41, 13, 10, 9, 9, 125, 13, 10, 9, 9, 105, 102, 32, 111, 112, 116, 105, 111, 110, 115, 46, 80, 114, 111, 106, 101, 99, 116, 73, 100, 32, 33, 61, 32, 110, 105, 108, 32, 123, 13, 10, 9, 9, 9, 114, 101, 113, 80, 97, 114, 97, 109, 91, 34, 112, 114, 111, 106, 101, 99, 116, 95, 105, 100, 34, 93, 32, 61, 32, 105, 110, 116, 101, 114, 110, 97, 108, 46, 83, 108, 105, 99, 101, 73, 110, 116, 84, 111, 83, 116, 114, 105, 110, 103, 40, 111, 112, 116, 105, 111, 110, 115, 46, 80, 114, 111, 106, 101, 99, 116, 73, 100, 44, 32, 34, 44, 34, 41, 13, 10, 9, 9, 125, 13, 10, 9, 125, 10, 13, 10, 9, 118, 97, 114, 32, 100, 97, 116, 97, 32, 42, 119, 103, 110, 46, 87, 103, 116, 118, 86, 101, 104, 105, 99, 108, 101, 115, 10, 9, 9, 118, 97, 114, 32, 109, 101, 116, 97, 68, 97, 116, 97, 32, 42, 71, 101, 110, 101, 114, 105, 99, 77, 101, 116, 97, 13, 10, 9, 101, 114, 114, 32, 58, 61, 32, 115, 101, 114, 118, 105, 99, 101, 46, 99, 108, 105, 101, 110, 116, 46, 103, 101, 116, 82, 101, 113, 117, 101, 115, 116, 40, 99, 116, 120, 44, 32, 115, 101, 99, 116, 105, 111, 110, 87, 103, 110, 44, 32, 114, 101, 97, 108, 109, 44, 32, 34, 47, 119, 103, 116, 118, 47, 118, 101, 104, 105, 99, 108, 101, 115, 47, 34, 44, 32, 114, 101, 113, 80, 97, 114, 97, 109, 44, 32, 38, 100, 97, 116, 97, 44, 32, 38, 109, 101, 116, 97, 68, 97, 116, 97, 41, 13, 10, 9, 114, 101, 116, 117, 114, 110, 32, 100, 97, 116, 97, 44, 32, 109, 101, 116, 97, 68, 97, 116, 97, 44, 32, 101, 114, 114, 13, 10, 125, 13, 10}
func main() {
fmt.Println("SOURCE:")
fmt.Println("###################")
fmt.Print(string(arr))
fmt.Println("###################")
content, _ := format.Source(arr)
fmt.Print(string(content))
fmt.Println("###################")
fmt.Println("correct result:")
fmt.Println("###################")
content, _ = format.Source(content)
fmt.Print(string(content))
fmt.Println("###################")
}
https://go.dev/play/p/KtdOV-TRZZZ
你期望看到什么?
// Auto generated file!
package wargaming
import (
"context"
"github.com/IceflowRE/go-wargaming/v4/wargaming/internal"
"github.com/IceflowRE/go-wargaming/v4/wargaming/wgn"
)
// WgtvVehicles returns list of vehicles sorted by games and covered by videos.
//
// https://developers.wargaming.net/reference/all/wgn/wgtv/vehicles
//
// realm:
//
// Valid realms: RealmAsia, RealmEu, RealmNa
func (service *WgnService) WgtvVehicles(ctx context.Context, realm Realm, options *wgn.WgtvVehiclesOptions) (*wgn.WgtvVehicles, *GenericMeta, error) {
if err := validateRealm(realm, []Realm{RealmAsia, RealmEu, RealmNa}); err != nil {
return nil, nil, err
}
reqParam := map[string]string{}
if options != nil {
if options.CategoryId != nil {
reqParam["category_id"] = internal.SliceIntToString(options.CategoryId, ",")
}
if options.ProgramId != nil {
reqParam["program_id"] = internal.SliceIntToString(options.ProgramId, ",")
}
if options.ProjectId != nil {
reqParam["project_id"] = internal.SliceIntToString(options.ProjectId, ",")
}
}
var data *wgn.WgtvVehicles
var metaData *GenericMeta
err := service.client.getRequest(ctx, sectionWgn, realm, "/wgtv/vehicles/", reqParam, &data, &metaData)
return data, metaData, err
}
你实际看到了什么?
// Auto generated file!
package wargaming
import (
"context"
"github.com/IceflowRE/go-wargaming/v4/wargaming/internal"
"github.com/IceflowRE/go-wargaming/v4/wargaming/wgn"
)
// WgtvVehicles returns list of vehicles sorted by games and covered by videos.
//
// https://developers.wargaming.net/reference/all/wgn/wgtv/vehicles
//
// realm:
// Valid realms: RealmAsia, RealmEu, RealmNa
func (service *WgnService) WgtvVehicles(ctx context.Context, realm Realm, options *wgn.WgtvVehiclesOptions) (*wgn.WgtvVehicles, *GenericMeta, error) {
if err := validateRealm(realm, []Realm{RealmAsia, RealmEu, RealmNa}); err != nil {
return nil, nil, err
}
reqParam := map[string]string{}
if options != nil {
if options.CategoryId != nil {
reqParam["category_id"] = internal.SliceIntToString(options.CategoryId, ",")
}
if options.ProgramId != nil {
reqParam["program_id"] = internal.SliceIntToString(options.ProgramId, ",")
}
if options.ProjectId != nil {
reqParam["project_id"] = internal.SliceIntToString(options.ProjectId, ",")
}
}
var data *wgn.WgtvVehicles
var metaData *GenericMeta
err := service.client.getRequest(ctx, sectionWgn, realm, "/wgtv/vehicles/", reqParam, &data, &metaData)
return data, metaData, err
}
6条答案
按热度按时间z8dt9xmd1#
我认为这是来自缩进行中的\r。
vmpqdwk32#
cc @griesemer
xurqigkl3#
感谢seankhliao提供的简化示例,非常有帮助。看起来
\r
确实导致了这个问题。顺便说一下,我们知道gofmt不是幂等的。
lsmepo6l4#
如果你感兴趣的话,可以关注@mvdan。
8nuwlpux5#
事实上,我对此很感兴趣 :)
顺便说一下,我们知道gofmt不是幂等的。这意味着它一直是一个已知的问题,你对这个限制感到满意,或者有幂等性bug应该尽快修复?
我一直认为gofmt应该是幂等的。实际上,我在gofmt中有很多幂等性问题,这让我担心。也许在使gofmt正确地具有幂等性之前尝试修复这些问题可能不是一个好主意。
附注:我觉得模糊测试可能会有助于发现幂等性问题。给任何可以解析为有效Go的输入进行第二次格式化,使用
go/format
应该导致零次更改。jaxagkaj6#
如果能修复幂等性bug,那就太好了——但我认为这样做并不简单。
可能针对当前问题有一个更针对性的修复方法,但我没有研究过。
同意模糊测试是有帮助的。