golang之反射和斷言的具體使用
1. 反射
反射這個(gè)概念絕大多數(shù)語(yǔ)言都有,比如Java,PHP之類,golang自然也不例外,反射其實(shí)程序能夠自描述和自控制的一類機(jī)制。
比如,通過(guò)PHP的反射,你可以知道一個(gè)類有什么成員,有什么方法。而golang,也能夠通過(guò)官方自帶的reflect包來(lái)了解各種變量類型及其信息。
下面我們通過(guò)一個(gè)例子查看反射的基本用法。
話不多說(shuō),直接貼代碼:
package main
import (
"fmt"
"reflect"
)
type Order struct {
ordId int `json:"order_id" validate:"required"`
customerId string `json:"customer_id" validate:"required"`
callback func() `json:"call_back" validate:"required"`
}
func reflectInfo(q interface{}) {
t := reflect.TypeOf(q)
v := reflect.ValueOf(q)
fmt.Println("Type ", t)
fmt.Println("Value ", v)
for i := 0; i < v.NumField(); i = i + 1 {
fv := v.Field(i)
ft := t.Field(i)
tag := t.Field(i).Tag.Get("json")
validate := t.Field(i).Tag.Get("validate")
switch fv.Kind() {
case reflect.String:
fmt.Printf("The %d th %s types: %s, valuing: %s, struct tag: %v\n", i, ft.Name, "string", fv.String(), tag + " " + validate)
case reflect.Int:
fmt.Printf("The %d th %s types %s, valuing %d, struct tag: %v\n", i, ft.Name, "int", fv.Int(), tag + " " + validate)
case reflect.Func:
fmt.Printf("The %d th %s types %s, valuing %v, struct tag: %v\n", i, ft.Name, "func", fv.String(), tag + " " + validate)
}
}
}
func main() {
o := Order{
ordId: 456,
customerId: "39e9e709-dd4f-0512-9488-a67c508b170f",
}
reflectInfo(o)
}
首先,我們用reflect.TypeOf(q)和reflect.ValueOf(q)獲取了結(jié)構(gòu)體order的類型和值,然后我們?cè)購(gòu)难h(huán)里對(duì)它的成員進(jìn)行一個(gè)遍歷,并將所有成員的名稱和類型打印了出來(lái)。這樣,一個(gè)結(jié)構(gòu)體的所有信息就都暴露在我們面前。
2.斷言
Go語(yǔ)言里面有一個(gè)語(yǔ)法,可以直接判斷是否是該類型的變量: value, ok = element.(T),這里value就是變量的值,ok是一個(gè)bool類型,element是interface變量,T是斷言的類型。
如果element里面確實(shí)存儲(chǔ)了T類型的數(shù)值,那么ok返回true,否則返回false。
package main
import (
"fmt"
)
type Order struct {
ordId int
customerId int
callback func()
}
func main() {
var i interface{}
i = Order{
ordId: 456,
customerId: 56,
}
value, ok := i.(Order)
if !ok {
fmt.Println("It's not ok for type Order")
return
}
fmt.Println("The value is ", value)
}
輸出:
The value is {456 56 <nil>}
常見(jiàn)的還有用switch來(lái)斷言:
package main
import (
"fmt"
)
type Order struct {
ordId int
customerId int
callback func()
}
func main() {
var i interface{}
i = Order{
ordId: 456,
customerId: 56,
}
switch value := i.(type) {
case int:
fmt.Printf("It is an int and its value is %d\n", value)
case string:
fmt.Printf("It is a string and its value is %s\n", value)
case Order:
fmt.Printf("It is a Order and its value is %v\n", value)
default:
fmt.Println("It is of a different type")
}
}
輸出:
It is a Order and its value is {456 56 <nil>}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
源碼解析gtoken替換jwt實(shí)現(xiàn)sso登錄
這篇文章主要為大家介紹了源碼解析gtoken替換jwt實(shí)現(xiàn)sso登錄的示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Golang 實(shí)現(xiàn)Socket服務(wù)端和客戶端使用TCP協(xié)議通訊
這篇文章主要介紹了Golang 實(shí)現(xiàn)Socket服務(wù)端和客戶端使用TCP協(xié)議通訊,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12
Golang限流器time/rate設(shè)計(jì)與實(shí)現(xiàn)詳解
在?Golang?庫(kù)中官方給我們提供了限流器的實(shí)現(xiàn)golang.org/x/time/rate,它是基于令牌桶算法(Token?Bucket)設(shè)計(jì)實(shí)現(xiàn)的,下面我們就來(lái)看看他的具體使用吧2024-03-03

