type Person struct{
Name string`info:"这是名称" doc:"我的名字"`
Sex bool`info:"这是性别"`}funcfindTag(obj interface{}){
t := reflect.TypeOf(obj).Elem()for i :=0; i < t.NumField(); i++{
tagInfo := t.Field(i).Tag.Get("info")
docInfo := t.Field(i).Tag.Get("doc")
fmt.Println("info:", tagInfo,",doc:", docInfo)}}funcmain(){
p := Person{
Name:"zzs",
Sex:false,}findTag(&p)}
GoLang 结构体标签在json中的使用
type MyPerson struct{
Name string`json:"name"`
Sex bool`json:"sex"`
Age int`json:"age"`
Phone []int`json:"phone"`}funcmain(){
mp :=&MyPerson{
Name:"Zzs",
Sex:false,
Age:21,
Phone:[]int{12344,123,45565,8956},}
strByte, err := json.Marshal(mp)if err !=nil{
fmt.Println(err)return}
fmt.Println(string(strByte))
newMp :=&MyPerson{}
err = json.Unmarshal(strByte, newMp)if err !=nil{
fmt.Println(err)return}
fmt.Println(newMp.Name)
fmt.Println(newMp.Age)
fmt.Println(newMp.Sex)
fmt.Println(newMp.Phone)}
Channel关闭
Channel与Select
事例
functest(c, quit chanint){
x, y :=1,1fortrue{select{case c <- x:
x = y
y += x
case<-quit:
fmt.Println("Quit")return}}}funcmain(){
c :=make(chanint)
quit :=make(chanint)gofunc(){for i :=0; i <10; i++{
fmt.Println(<-c)}
quit <-0}()test(c, quit)}
GoLang 知识点补充(二)GoLang 反射解析结构体标签type Person struct { Name string `info:"这是名称" doc:"我的名字"` Sex bool `info:"这是性别"`}func findTag(obj interface{}) { t := reflect.TypeOf(obj).Elem() for i := 0; i < t.NumField(); i++ { tagInfo := t.Field(i).Tag.G