Standard library / reflect

reflect.Value.FieldByName()

Return a struct field by name, including unambiguous promoted fields. Missing fields return an invalid Value. Expected stdout: '7 false\n'.

Formal Syntax & Structure

go syntaxgo
func (v Value) FieldByName(name string) Value

Parameters & Attributes

NameTypeDescription
namestringExact field or method name.

Return Value

Value

Return a struct field by name, including unambiguous promoted fields.

Working Example

Example Snippetgo
package main
import (  "fmt"  "reflect")
func main() {  v := reflect.ValueOf(struct{ X int }{7})  fmt.Println(v.FieldByName("X").Int(), v.FieldByName("Y").IsValid())}
Go Reference