mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 05:33:04 +08:00
32 lines
535 B
Go
32 lines
535 B
Go
// 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}
|
|
*/
|