mirror of
https://github.com/golang/go
synced 2024-11-17 02:14:42 -07: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:
parent
a6c75aa5c3
commit
81a9a7f4c2
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user