aboutsummaryrefslogtreecommitdiff
path: root/src/reflect
diff options
context:
space:
mode:
authorMostafa Solati <mostafa.solati@gmail.com>2020-06-08 22:04:09 +0430
committerTobias Klauser <tobias.klauser@gmail.com>2021-08-22 13:14:19 +0000
commitbd6845965c298b250f8af577a24416de0661c53d (patch)
tree130b06732ddc0fdb4a8d0b9197b5308ca04be2a9 /src/reflect
parent96d816c5740f7576ed0f6346f70958ce9ef1e3b4 (diff)
downloadgo-bd6845965c298b250f8af577a24416de0661c53d.tar.gz
go-bd6845965c298b250f8af577a24416de0661c53d.zip
reflect: add example for FieldByIndex
Change-Id: I539453e50ab85ec1b023bc9e329e6451c674e0c9 Reviewed-on: https://go-review.googlesource.com/c/go/+/236937 Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com> TryBot-Result: Go Bot <gobot@golang.org>
Diffstat (limited to 'src/reflect')
-rw-r--r--src/reflect/example_test.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/reflect/example_test.go b/src/reflect/example_test.go
index 23c08e4950..684bafd648 100644
--- a/src/reflect/example_test.go
+++ b/src/reflect/example_test.go
@@ -166,3 +166,31 @@ func ExampleStructOf() {
// json: {"height":0.4,"age":2}
// value: &{Height:1.5 Age:10}
}
+
+func ExampleValue_FieldByIndex() {
+ // This example shows a case in which the name of a promoted field
+ // is hidden by another field: FieldByName will not work, so
+ // FieldByIndex must be used instead.
+ type user struct {
+ firstName string
+ lastName string
+ }
+
+ type data struct {
+ user
+ firstName string
+ lastName string
+ }
+
+ u := data{
+ user: user{"Embedded John", "Embedded Doe"},
+ firstName: "John",
+ lastName: "Doe",
+ }
+
+ s := reflect.ValueOf(u).FieldByIndex([]int{0, 1})
+ fmt.Println("embedded last name:", s)
+
+ // Output:
+ // embedded last name: Embedded Doe
+}