GO語言"reflect"包中"StructTag.Lookup"類型的用法及代碼示例。
用法:
func(tag StructTag) Lookup(key string)(value string, ok bool)
查找返回與標簽字符串中的鍵關聯的值。如果標簽中存在鍵,則返回值(可能為空)。否則返回值將是空字符串。 ok 返回值報告該值是否在標記字符串中顯式設置。如果標簽沒有常規格式,則 Lookup 返回的值是未指定的。
例子:
package main
import (
"fmt"
"reflect"
)
func main() {
type S struct {
F0 string `alias:"field_0"`
F1 string `alias:""`
F2 string
}
s := S{}
st := reflect.TypeOf(s)
for i := 0; i < st.NumField(); i++ {
field := st.Field(i)
if alias, ok := field.Tag.Lookup("alias"); ok {
if alias == "" {
fmt.Println("(blank)")
} else {
fmt.Println(alias)
}
} else {
fmt.Println("(not specified)")
}
}
}
輸出:
field_0 (blank) (not specified)
相關用法
- GO StructTag用法及代碼示例
- GO StructOf用法及代碼示例
- GO StreamWriter用法及代碼示例
- GO Strings用法及代碼示例
- GO Stringer用法及代碼示例
- GO StripPrefix用法及代碼示例
- GO StreamReader用法及代碼示例
- GO Stmt用法及代碼示例
- GO Stmt.QueryRowContext用法及代碼示例
- GO Scanner.Scan用法及代碼示例
- GO Split用法及代碼示例
- GO Server.Shutdown用法及代碼示例
- GO Slice用法及代碼示例
- GO SplitAfter用法及代碼示例
- GO Sum256用法及代碼示例
- GO SectionReader用法及代碼示例
- GO Sin用法及代碼示例
- GO Sprintf用法及代碼示例
- GO SendMail用法及代碼示例
- GO Sprint用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 StructTag.Lookup。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。