Home > Enterprise >  Struct field tag `name` not compatible with reflect.StructTag.Get: bad syntax for struct tag pair
Struct field tag `name` not compatible with reflect.StructTag.Get: bad syntax for struct tag pair

Time:06-30

I read enter image description here

What could be the issue?

CodePudding user response:

Note that that's just a warning telling you that you're not following convention. The code, as you already know, compiles and runs and outputs the result you want: https://go.dev/play/p/gxcv8qPVZ6z.

To avoid the warning, disable your linter, or, better yet, follow the convention by using key:"value" in the struct tags and then extract the value by using the Get method: https://go.dev/play/p/u0VTGL48TjO.


https://pkg.go.dev/[email protected]#StructTag

A StructTag is the tag string in a struct field.

By convention, tag strings are a concatenation of optionally space-separated key:"value" pairs. Each key is a non-empty string consisting of non-control characters other than space (U 0020 ' '), quote (U 0022 '"'), and colon (U 003A ':'). Each value is quoted using U 0022 '"' characters and Go string literal syntax.

CodePudding user response:

Struct tag supposed to be a key:"value", field:"name" for example.

type User struct {
    Name string `field:"name"`
    Age  int64  `field:"age"`
}
  • Related