Standard library / reflect

reflect.Value.FieldByIndex()

Follow a nested field index path; following a nil embedded pointer panics. Expected stdout: '7\n'.

Formal Syntax & Structure

go syntaxgo
func (v Value) FieldByIndex(index []int) Value

Parameters & Attributes

NameTypeDescription
index[]intSequence of zero-based field indexes through nested structs.

Return Value

Value

Follow a nested field index path; following a nil embedded pointer panics.

Working Example

Example Snippetgo
package main
import (  "fmt"  "reflect")
func main() {  type A struct{ X int }  type B struct{ A }  v := reflect.ValueOf(B{A{7}})  fmt.Println(v.FieldByIndex([]int{0, 0}).Int())}
Go Reference