如何在Go中使用“类型”作为结构属性?

"type" is a language keyword but I need to use it as an attribute name in my structure like :

type Message struct{
    type string
}

My IDE finds an error line 2

Why do you need to use type?

When you need to parse JSON-encoded data with this attribute, you can write the following:

type Message struct {
  Kind string `json:"type"`
}

type is a keyword in Go, so you can't use it as an identifier. As an alternative, you can use:

  • exported Type
  • type_
  • typ

All of these I got from the Go source code.

type is a reserved word and may not be used as identifiers. Docs