mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 04:48:29 +08:00
fix: coding style and file format for all example.
This commit is contained in:
@@ -1,31 +1,32 @@
|
||||
// reflect_struct2.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type T struct {
|
||||
A int
|
||||
B string
|
||||
}
|
||||
|
||||
func main() {
|
||||
t := T{23, "skidoo"}
|
||||
s := reflect.ValueOf(&t).Elem()
|
||||
typeOfT := s.Type()
|
||||
for i := 0; i < s.NumField(); i++ {
|
||||
f := s.Field(i)
|
||||
fmt.Printf("%d: %s %s = %v\n", i,
|
||||
typeOfT.Field(i).Name, f.Type(), f.Interface())
|
||||
}
|
||||
s.Field(0).SetInt(77)
|
||||
s.Field(1).SetString("Sunset Strip")
|
||||
fmt.Println("t is now", t)
|
||||
}
|
||||
/* Output:
|
||||
0: A int = 23
|
||||
1: B string = skidoo
|
||||
t is now {77 Sunset Strip}
|
||||
*/
|
||||
// reflect_struct2.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type T struct {
|
||||
A int
|
||||
B string
|
||||
}
|
||||
|
||||
func main() {
|
||||
t := T{23, "skidoo"}
|
||||
s := reflect.ValueOf(&t).Elem()
|
||||
typeOfT := s.Type()
|
||||
for i := 0; i < s.NumField(); i++ {
|
||||
f := s.Field(i)
|
||||
fmt.Printf("%d: %s %s = %v\n", i,
|
||||
typeOfT.Field(i).Name, f.Type(), f.Interface())
|
||||
}
|
||||
s.Field(0).SetInt(77)
|
||||
s.Field(1).SetString("Sunset Strip")
|
||||
fmt.Println("t is now", t)
|
||||
}
|
||||
|
||||
/* Output:
|
||||
0: A int = 23
|
||||
1: B string = skidoo
|
||||
t is now {77 Sunset Strip}
|
||||
*/
|
||||
|
Reference in New Issue
Block a user