var arr [10]int arr[0] = 10 arr[1] = 20 arr[2] = 30 arr[3] = 40
type Animal struct { Name string age int } var a Animal
package main import ( "fmt" "reflect" ) func main() { var x float64 = 3.4 fmt.Println("type:", reflect.TypeOf(x)) }
package main import ( "fmt" "reflect" ) func main() { var x float64 = 3.4 t := reflect.TypeOf(x) fmt.Println("type:", t.Kind()) }
var x float64 = 3.4 v := reflect.ValueOf(x) // 和reflect.TypeOf功能是同樣的 fmt.Println("type:", v.Type()) fmt.Println("kind is float64:",v.Kind() == reflect.Float64) fmt.Println("value:",v.Float())
var x float64 = 3.4 v := reflect.ValueOf(x) fmt.Println("type:", v.Type()) fmt.Println("kind is float64:",v.Kind() == reflect.Float64) v.SetFloat(6.8) fmt.Println("value:", v.Float())
panic,程序崩潰了。sql
var x float64 = 3.4 // 傳地址進去,不傳地址的話,改變的是副本的值 // 因此在reflect包裏直接崩潰了!!!! v := reflect.ValueOf(&x) fmt.Println("type:", v.Type()) fmt.Println("kind is float64:", v.Kind() == reflect.Float64) v.SetFloat(6.8) fmt.Println("value:",v.Float())
我靠,還報錯.數據庫
var x float64 = 3.4 // 傳地址進去,不傳地址的話,改變的是副本的值 // 因此在reflect包裏直接崩潰了!!!! v := reflect.ValueOf(&x) fmt.Println("type:",v.Type()) fmt.Println("kind is float64:",v.Kind() == reflect.Float64) // 經過Elem()獲取指針指向的變量,從而完成賦值操做。 // 正常操做是經過*號來解決的,好比 // var *p int = new(int) // *p = 100 v.Elem().SetFloat(6.8) fmt.Println("value:",v.Float())
var x float64 = 3.4 v := reflect.ValueOf(&x) fmt.Println("type:",v.Type()) fmt.Println("kind is float64:",v.Kind() == reflect.Float64) v.Elem().SetInt(100) fmt.Println("value:",v.Float()) // 我靠,又犯賤了。
package main import ( "fmt" "reflect" ) type S struct { A int B string } func main() { s := S{23, "skidoo"} v := reflect.ValueOf(s) t := v.Type() for i := 0; i < v.NumField(); i++ { f := v.Field(i) fmt.Printf("%d: %s %s = %v\n",i,t.Field(i).Name, f.Type(), f.Interface()) } }
package main import ( "fmt" "reflect" ) type S struct { A int B string } func main() { s := S{23, "skidoo"} v := reflect.ValueOf(s) t := v.Type() for i := 0; i < v.NumField(); i++ { f := v.Field(i) fmt.Printf("%d: %s %s = %v\n",i,t.Field(i).Name, f.Type(), f.Interface()) } }
package main import ( "fmt" "reflect" ) type S struct { A int B string } func main() { s := S{23, "skidoo"} v := reflect.ValueOf(&s) t := v.Type() v.Elem().Field(0).SetInt(100) for i := 0; i < v.Elem().NumField(); i++ { f := v.Elem().Field(i) fmt.Printf("%d: %s %s = %v\n",i, t.Elem().Field(i).Name, f.Type(), f.Interface()) } }
package main import ( "fmt" "reflect" ) type S struct { A int B string } func (s *S) Test() { fmt.Println("this is a test") } func main() { s := S{23, "skidoo"} v := reflect.ValueOf(&s) t := v.Type() v.Elem().Field(0).SetInt(100) fmt.Println("method num:", v.NumField()) for i := 0; i < v.NumMethod(); i++ { f := t.Method(i) fmt.Printf("%d method, name:%v, type:%v\n", i, f.Name, f.Type) } }
package main import ( "fmt" "reflect" ) type S struct { A int B string } func (s *S) Test() { fmt.Println("this is a test") } func (s *S) SetA(a int) { s.A = a } func main() { s := S{23, "skidoo"} v := reflect.ValueOf(&s) m := v.MethodByName("Test") var args1 []reflect.Value m.Call(args1) setA := v.MethodByName("SetA") var args2 []reflect.Value args2 = append(args2, reflect.ValueOf(100)) setA.Call(args2) fmt.Printf("s:%#v\n",s) }
package main import ( "fmt" "reflect" ) type S struct { F string `species:"gopher" color:"blue" json:"f"` } func main() { s := S{} st := reflect.TypeOf(s) field := st.Field(0) fmt.Println(field.Tag.Get("color"), field.Tag.Get("species"), field.Tag.Get("json")) }
在運行時動態獲取一個變量的類型信息和值信息就叫作反射json