How to get rid of Go vet warning % in Println

我怕爱的太早我们不能终老 提交于 2020-12-26 07:43:51

问题


This code

package main

import (
    "fmt"
)

func main() {
    fmt.Println("%%dude")
}

Playground link: https://play.golang.org/p/Shq5pMHg4bj

gives a go vet warning

./prog.go:8:2: Println call has possible formatting directive %d

How can I tell go vet that I really want to write two percent signs and not to warn me?


回答1:


You can't really supress that, but even if you could with custom rules and flags, I wouldn't do it because someone else building your code will still run into this.

Instead you may use any of these alternatives which produce the same output without any warnings from vet:

fmt.Println("%%"+"dude")
fmt.Println("%\x25dude")
fmt.Printf("%%%%dude\n")
s := "%%dude"
fmt.Println(s)

Try the examples on the Go Playground.




回答2:


You can't really (apart from not writing that code). Go vet doesn't have any mechanism for "I really meant this" comments to suppress warnings. There have been several discussions about it in the past five years, none of which have resulted in any action. You just have to accept what the help text says: go vet "uses heuristics that do not guarantee all reports are genuine problems".



来源:https://stackoverflow.com/questions/65230133/how-to-get-rid-of-go-vet-warning-in-println

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!