Skip to content

proposal: Go 2: obtain struct field name in type-safe way #36332

Closed
@lifeng1992

Description

@lifeng1992
type Target struct {
    Name     string
    Path     string
    Date     time.Time
}

func main()  {
    ^Target{Name, Path} // equivalent []string{"Name", "Path"}
    ^Target{}.Name // equivalent "Name"
}

Good point

  • compatible with Go 1.x
  • just syntactic sugar
  • refactor friendly
  • avoid typo

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions