Standard library / reflect

reflect.Value.Field()

Return the direct field at an index in a struct value. Incorrect kind or out-of-range index panics. Expected stdout: '7\n'.

Formal Syntax & Structure

go syntaxgo
func (v Value) Field(i int) Value

Parameters & Attributes

NameTypeDescription
iintZero-based index, or the input interface value for TypeOf and ValueOf.

Return Value

Value

Return the direct field at an index in a struct value.

Working Example

Example Snippetgo
package main
import (  "fmt"  "reflect")
func main() { v := reflect.ValueOf(struct{ X int }{7}); fmt.Println(v.Field(0).Int()) }
Go Reference