Standard library / reflect

reflect.Value.FieldByNameFunc()

Find a struct field whose name satisfies the predicate. Ambiguous or absent matches yield an invalid Value. Expected stdout: '7\n'.

Formal Syntax & Structure

go syntaxgo
func (v Value) FieldByNameFunc(match func(string) bool) Value

Parameters & Attributes

NameTypeDescription
matchfunc(string) boolPredicate applied to candidate names.

Return Value

Value

Find a struct field whose name satisfies the predicate.

Working Example

Example Snippetgo
package main
import (  "fmt"  "reflect"  "strings")
func main() {  v := reflect.ValueOf(struct{ Count int }{7})  fmt.Println(v.FieldByNameFunc(func(s string) bool { return strings.HasPrefix(s, "Cou") }).Int())}
Go Reference