1
0
mirror of https://github.com/golang/go synced 2024-09-29 07:24:32 -06:00

reflect: add example for FieldByName

Change-Id: I47e1cc261fdcd6f83a8593893b979d130150d0b2
Reviewed-on: https://go-review.googlesource.com/c/go/+/407174
Auto-Submit: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
This commit is contained in:
Mostafa Solati 2022-05-19 02:55:03 +04:30 committed by Gopher Robot
parent a6c75aa5c3
commit 81a9a7f4c2

View File

@ -194,3 +194,16 @@ func ExampleValue_FieldByIndex() {
// Output:
// embedded last name: Embedded Doe
}
func ExampleValue_FieldByName() {
type user struct {
firstName string
lastName string
}
u := user{firstName: "John", lastName: "Doe"}
s := reflect.ValueOf(u)
fmt.Println("Name:", s.FieldByName("firstName"))
// Output:
// Name: John
}