123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
package mainimport ("fmt""reflect""time")func main() {checkCanAddr()}type S struct {X intY stringz int}func M() int {return 100}var x0 = 0func checkCanAddr() {// 可定址的情況v := reflect.ValueOf(x0)fmt.Printf("x0: %v \tcan be addressable and set: %t, %t\n", x0, v.CanAddr(), v.CanSet()) //false,falsevar x1 = 1v = reflect.Indirect(reflect.ValueOf(x1))fmt.Printf("x1: %v \tcan be addressable and set: %t, %t\n", x1, v.CanAddr(), v.CanSet()) //false,falsevar x2 = &x1v = reflect.Indirect(reflect.ValueOf(x2))fmt.Printf("x2: %v \tcan be addressable and set: %t, %t\n", x2, v.CanAddr(), v.CanSet()) //true,truevar x3 = time.Now()v = reflect.Indirect(reflect.ValueOf(x3))fmt.Printf("x3: %v \tcan be addressable and set: %t, %t\n", x3, v.CanAddr(), v.CanSet()) //false,falsevar x4 = &x3v = reflect.Indirect(reflect.ValueOf(x4))fmt.Printf("x4: %v \tcan be addressable and set: %t, %t\n", x4, v.CanAddr(), v.CanSet()) // true,truevar x5 = []int{1, 2, 3}v = reflect.ValueOf(x5)fmt.Printf("x5: %v \tcan be addressable and set: %t, %t\n", x5, v.CanAddr(), v.CanSet()) // false,falsevar x6 = []int{1, 2, 3}v = reflect.ValueOf(x6[0])fmt.Printf("x6: %v \tcan be addressable and set: %t, %t\n", x6[0], v.CanAddr(), v.CanSet()) //false,falsevar x7 = []int{1, 2, 3}v = reflect.ValueOf(x7).Index(0)fmt.Printf("x7: %v \tcan be addressable and set: %t, %t\n", x7[0], v.CanAddr(), v.CanSet()) //true,truev = reflect.ValueOf(&x7[1])fmt.Printf("x7.1: %v \tcan be addressable and set: %t, %t\n", x7[1], v.CanAddr(), v.CanSet()) //true,truevar x8 = [3]int{1, 2, 3}v = reflect.ValueOf(x8[0])fmt.Printf("x8: %v \tcan be addressable and set: %t, %t\n", x8[0], v.CanAddr(), v.CanSet()) //false,false// https://groups.google.com/forum/#!topic/golang-nuts/RF9zsX82MWwvar x9 = [3]int{1, 2, 3}v = reflect.Indirect(reflect.ValueOf(x9).Index(0))fmt.Printf("x9: %v \tcan be addressable and set: %t, %t\n", x9[0], v.CanAddr(), v.CanSet()) //false,falsevar x10 = [3]int{1, 2, 3}v = reflect.Indirect(reflect.ValueOf(&x10)).Index(0)fmt.Printf("x9: %v \tcan be addressable and set: %t, %t\n", x10[0], v.CanAddr(), v.CanSet()) //true,truevar x11 = S{}v = reflect.ValueOf(x11)fmt.Printf("x11: %v \tcan be addressable and set: %t, %t\n", x11, v.CanAddr(), v.CanSet()) //false,falsevar x12 = S{}v = reflect.Indirect(reflect.ValueOf(&x12))fmt.Printf("x12: %v \tcan be addressable and set: %t, %t\n", x12, v.CanAddr(), v.CanSet()) //true,truevar x13 = S{}v = reflect.ValueOf(x13).FieldByName("X")fmt.Printf("x13: %v \tcan be addressable and set: %t, %t\n", x13, v.CanAddr(), v.CanSet()) //false,falsevar x14 = S{}v = reflect.Indirect(reflect.ValueOf(&x14)).FieldByName("X")fmt.Printf("x14: %v \tcan be addressable and set: %t, %t\n", x14, v.CanAddr(), v.CanSet()) //true,truevar x15 = S{}v = reflect.Indirect(reflect.ValueOf(&x15)).FieldByName("z")fmt.Printf("x15: %v \tcan be addressable and set: %t, %t\n", x15, v.CanAddr(), v.CanSet()) //true,falsev = reflect.Indirect(reflect.ValueOf(&S{}))fmt.Printf("x15.1: %v \tcan be addressable and set: %t, %t\n", &S{}, v.CanAddr(), v.CanSet()) //true,truevar x16 = Mv = reflect.ValueOf(x16)fmt.Printf("x16: %p \tcan be addressable and set: %t, %t\n", x16, v.CanAddr(), v.CanSet()) //false,falsevar x17 = Mv = reflect.Indirect(reflect.ValueOf(&x17))fmt.Printf("x17: %p \tcan be addressable and set: %t, %t\n", x17, v.CanAddr(), v.CanSet()) //true,truevar x18 interface{} = &x11v = reflect.ValueOf(x18)fmt.Printf("x18: %v \tcan be addressable and set: %t, %t\n", x18, v.CanAddr(), v.CanSet()) //false,falsevar x19 interface{} = &x11v = reflect.ValueOf(x19).Elem()fmt.Printf("x19: %v \tcan be addressable and set: %t, %t\n", x19, v.CanAddr(), v.CanSet()) //true,truevar x20 = [...]int{1, 2, 3}v = reflect.ValueOf([...]int{1, 2, 3})fmt.Printf("x20: %v \tcan be addressable and set: %t, %t\n", x20, v.CanAddr(), v.CanSet()) //false,false} |