You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
8000
SA5008: false positive on json:"-,omitempty" struct tag #1699
However, since upgrading to staticcheck v0.7.0, I've been getting this false positive:
internal/json/json.go:112:33: should encoding/json ignore this field or name it "-"? Either use `json:"-"` to ignore the field or use `json:"'-',omitempty"` to specify "-" as the name (SA5008)
As near as I can tell, there's nothing wrong with the current construction. The suggested fix is new syntax that is only available if using the encoding/json/v2 package, which I am not.
Particulars
staticcheck -version: staticcheck 2026.1 (v0.7.0)
staticcheck -debug.version:
staticcheck 2026.1 (v0.7.0)
Compiled with Go version: go1.25.6
Main module:
honnef.co/go/tools@v0.7.0 (sum: h1:w6WUp1VbkqPEgLz4rkBzH/CSU6HkoqNLp6GstyTx3lU=)
Dependencies:
github.com/BurntSushi/toml@v1.4.1-0.20240526193622-a339e1f7089c (sum: h1:pxW6RcqyfI9/kWtOwnv/G+AzdKuy2ZrqINhenH4HyNs=)
golang.org/x/exp/typeparams@v0.0.0-20231108232855-2478ac86f678 (sum: h1:1P7xPZEwZMoBoz0Yze5Nx2/4pxj6nw9ZqHWXqP0iRgQ=)
golang.org/x/mod@v0.31.0 (sum: h1:HaW9xtz0+kOcWKwli0ZXy79Ix+UW/vOfmWI5QVd2tgI=)
golang.org/x/sync@v0.19.0 (sum: h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=)
golang.org/x/tools@v0.40.1-0.20260108161641-ca281cf95054 (sum: h1:CHVDrNHx9ZoOrNN9kKWYIbT5Rj+WF2rlwPkhbQQ5V4U=)
go version: go version go1.25.6 darwin/arm64
go env: ... please just let me know any relevant values I should post; I'm not going to post the full thing
Exactly which command you ran: staticcheck ./internal/json
Output of the command and what's wrong with the output: see above
When I made the change I checked that encoding/json also supports quoting the - when it's used as a field name. Did I make a mistake there?
As for the false positive, you're technically correct, but I've decided that the unquoted form is too prone to accidents, see also #1655. To make things more explicit, and to prepare for a potential migration to encoding/json/v2, we emit the diagnostic even for encoding/json.
Oh this is very annoying. Whether encoding/json accepts that syntax depends on the value of GOEXPERIMENT...
$ cat foo.go
package main
import (
"encoding/json"
"fmt"
"log"
)
type S struct {
Foo int `json:"'-'"`
}
func main() {
s := S{42}
j, err := json.Marshal(s)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(j))
}
$ go run foo.go
{"Foo":42}
$ GOEXPERIMENT=jsonv2 go run foo.go
{"-":42}
Thanks @dominikh, much appreciated. I was hopeful about using the '-' syntax as suggested by the linter myself but was disappointed to see that it only works in json/v2.
Activity
dominikh commentedon Feb 25, 2026
When I made the change I checked that
encoding/jsonalso supports quoting the-when it's used as a field name. Did I make a mistake there?As for the false positive, you're technically correct, but I've decided that the unquoted form is too prone to accidents, see also #1655. To make things more explicit, and to prepare for a potential migration to encoding/json/v2, we emit the diagnostic even for
encoding/json.dominikh commentedon Feb 25, 2026
Oh this is very annoying. Whether
encoding/jsonaccepts that syntax depends on the value ofGOEXPERIMENT...To be clear, this is something I need to fix.
patjakdev commentedon Feb 25, 2026
Thanks @dominikh, much appreciated. I was hopeful about using the
'-'syntax as suggested by the linter myself but was disappointed to see that it only works injson/v2.