topskip
topskip

Reputation: 17345

How to get rid of Go vet warning % in Println

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?

Upvotes: 3

Views: 1161

Answers (2)

icza
icza

Reputation: 417672

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.

Upvotes: 10

hobbs
hobbs

Reputation: 239920

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".

Upvotes: 3

Related Questions