golang反射包的使用

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

golang反射在某些特定的情境非常有用,比如我在web開發的時候,可以通過反射將表單映射到結構體上面,並能通過結構體定義的tag屬性自動驗證資料資料格式是否正確,如下例子:

我們可以將form表單

<form action="" method="POST">    <input type="text" name="Name" />    <input type="text" name="Age" /></form>

映射到下面這個結構體裡面去

type User struct{      Name string `valid:"String"`      Age int `valid:"Int"` }

 TypeOf 擷取類型資訊

ValueOf擷取值資訊

package mainimport ("fmt""reflect")type a struct {Name stringAge  string}func (this a) Test() {fmt.Println("test ok.")}func (this *a) Get() {fmt.Println("get ok.")}func (this *a) Print(id int, name string) {fmt.Println("id is ", id)fmt.Println("name is ", name)}func Test() {fmt.Println("func test is ok.")}func main() {d := &a{Name: "aaa", Age: "bbb"}v := reflect.ValueOf(d)ele := v.Elem()t := ele.Type()fmt.Println("\n讀取對象的所有屬性")for i := 0; i < t.NumField(); i++ {fmt.Println(t.Field(i).Name, ele.Field(i).Interface())}fmt.Println("\n讀取對象的所有方法")for i := 0; i < t.NumMethod(); i++ {fmt.Println(t.Method(i).Name)}//反射調用方法fmt.Println("\n測試函數調用,看來還是e保險一些,不會出錯")v.MethodByName("Get").Call(nil)v.MethodByName("Test").Call(nil)//使用ele.MethodByName("GET").Call(nil)會拋出異常//但是使用ele.MethodByName("Test").Call(nil)卻是正常?因為Test沒有用指標ele.MethodByName("Test").Call(nil)//調用有參數的反射fmt.Println("\n測試傳值調用函數")vId := reflect.ValueOf(88)vName := reflect.ValueOf("小汪")v.MethodByName("Print").Call([]reflect.Value{vId, vName})fmt.Println("\n測試參數賦值,必須要用ele")ele.FieldByName("Name").SetString("哈哈")fmt.Println(d)c := 123tc := reflect.ValueOf(&c).Elem()tc.SetInt(333)fmt.Println(tc, reflect.TypeOf(&c).Elem().Name())fmt.Println(c)}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.