]> Cypherpunks repositories - gostls13.git/commitdiff
reflect: add example for FieldByName
authorMostafa Solati <mostafa.solati@gmail.com>
Wed, 18 May 2022 22:25:03 +0000 (02:55 +0430)
committerGopher Robot <gobot@golang.org>
Thu, 19 May 2022 20:04:36 +0000 (20:04 +0000)
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>
src/reflect/example_test.go

index 3db971c3aeed7ab904fdc9cc93542383ef881b70..b4f3b2932f78c368492d5d46d86319776676ce77 100644 (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
+}