• 遍历切片slice,结构体struct,映射map,interface{}的属性和值


    1 前言

    说明:interface{}必须是前三者类型

    2 代码

    /**
     * @Author: FB
     * @Description: 
     * @File:  testOutput.go
     * @Version: 1.0.0
     * @Date: 2019/6/3 10:38
     */
    
    package main
    
    import (
       "reflect"
       "fmt"
    )
    
    type NotknownType struct {
       s1, s2, s3 string
    }
    
    type NotknownType2 struct {
       S1, S2, S3 string
    }
    
    //只输出值且属性名小写
    func output11(obj  NotknownType){
       value := reflect.ValueOf(obj)
       for i := 0; i < value.NumField(); i++ {
          fmt.Printf("Field %d: %v
    ", i, value.Field(i))
       }
    }
    
    //只输出属性和值且属性名小写
    func output12(obj  NotknownType){
       value := reflect.ValueOf(obj)
       t := reflect.TypeOf(obj)
       for i := 0; i < t.NumField(); i++ {
          fmt.Println(t.Field(i).Name, "->", value.Field(i).Interface())
       }
    }
    //只输出值且属性名大写
    func output21(obj  NotknownType2){
       value := reflect.ValueOf(obj)
       for i := 0; i < value.NumField(); i++ {
          fmt.Printf("Field %d: %v
    ", i, value.Field(i))
       }
    }
    //只输出属性和值且属性名大写
    func output22(obj  NotknownType2){
       value := reflect.ValueOf(obj)
       t := reflect.TypeOf(obj)
       for i := 0; i < t.NumField(); i++ {
          fmt.Println(t.Field(i).Name, "->", value.Field(i).Interface())
       }
    }
    
    //只输出属性和值且属性名大写
    func output221(obj  NotknownType2){
    		fmt.Printf("%+v",obj)
    		fmt.Println()
    }
    
    //以下是参数为interface{},和上面四个其它一一对应
    func output110(obj  interface{}){
       value := reflect.ValueOf(obj)
       for i := 0; i < value.NumField(); i++ {
          fmt.Printf("Field %d: %v
    ", i, value.Field(i))
       }
    }
    
    
    func output120(obj  interface{}){
       value := reflect.ValueOf(obj)
       t := reflect.TypeOf(obj)
       for i := 0; i < t.NumField(); i++ {
          fmt.Println(t.Field(i).Name, "->", value.Field(i).Interface())
       }
    }
    
    func output210(obj  interface{}){
       value := reflect.ValueOf(obj)
       for i := 0; i < value.NumField(); i++ {
          fmt.Printf("Field %d: %v
    ", i, value.Field(i))
       }
    }
    
    func output220(obj  interface{}){
       value := reflect.ValueOf(obj)
       t := reflect.TypeOf(obj)
       for i := 0; i < t.NumField(); i++ {
          fmt.Println(t.Field(i).Name, "->", value.Field(i).Interface())
       }
    }
    
    
    func main() {
    
       var secret = NotknownType{"Ada", "Go", "Oberon"}
       var secret2  = NotknownType2{"Ada", "Go", "Oberon"}
    
       var map1 = map[string]string{"s1":"Ada", "s2":"Go", "s3":"Oberon"}
       var slice1 = []string{"Ada", "Go", "Oberon"}
    
    
       var obj interface{} = NotknownType{"Ada", "Go", "Oberon"}
       var obj2 interface{} = NotknownType2{"Ada", "Go", "Oberon"}
    
       //参数是struct
       fmt.Println("output11只输出值且属性名小写")
       output11(secret)
       //报错panic: reflect.Value.Interface: cannot return value obtained from unexported field or method,因为属性是小写,反射拿不到值
       //fmt.Println("output12")
       //output12(secret)
       fmt.Println("output21只输出值且属性名大写")
       output21(secret2)
       fmt.Println("output22只输出属性和值且属性名大写")
       output22(secret2)
    
            fmt.Println()
    	fmt.Println("output221")
    	output221(secret)
    	fmt.Println("output222")
    	output222(secret2)
    	fmt.Println()
    
       //参数是interface{}
       fmt.Println("output110只输出值且属性名小写,参数为interface{}")
       output110(obj)
       //报错panic: reflect.Value.Interface: cannot return value obtained from unexported field or method,因为属性是小写,反射拿不到值
       //fmt.Println("output120")
       //output120(obj)
       fmt.Println("output210只输出值且属性名大写,参数为interface{}")
       output210(obj2)
       fmt.Println("output220只输出属性和值且属性名大写,参数为interface{}")
       output220(obj2)
    
    
       //报错panic: reflect: NumField of non-struct type
       //output220(map1)
       fmt.Println("output220 只输出属性和值且属性名大写,输入为struct属性大写")
       output220(secret2)
    
       fmt.Println("遍历map,输出键和值")
       //输出map键和值
       for k,v := range map1{
          fmt.Println(k,"->",v)
       }
    
       fmt.Println("遍历slice,输出值")
       //输出map键和值
       for i, v := range slice1{
          fmt.Println(i, "->", v)
       }
    
    
    }
    

    输出:

    output11只输出值且属性名小写
    Field 0: Ada
    Field 1: Go
    Field 2: Oberon
    output21只输出值且属性名大写
    Field 0: Ada
    Field 1: Go
    Field 2: Oberon
    output22只输出属性和值且属性名大写
    S1 -> Ada
    S2 -> Go
    S3 -> Oberon
    
    output221
    {s1:Ada s2:Go s3:Oberon}
    output222
    {S1:Ada S2:Go S3:Oberon}
    
    output110只输出值且属性名小写,参数为interface{}
    Field 0: Ada
    Field 1: Go
    Field 2: Oberon
    output210只输出值且属性名大写,参数为interface{}
    Field 0: Ada
    Field 1: Go
    Field 2: Oberon
    output220只输出属性和值且属性名大写,参数为interface{}
    S1 -> Ada
    S2 -> Go
    S3 -> Oberon
    output220 只输出属性和值且属性名大写,输入为struct属性大写
    S1 -> Ada
    S2 -> Go
    S3 -> Oberon
    遍历map,输出键和值
    s1 -> Ada
    s2 -> Go
    s3 -> Oberon
    遍历slice,输出值
    0 -> Ada
    1 -> Go
    2 -> Oberon
    
    Debugger finished with exit code 0
    

      

      

  • 相关阅读:
    快速排序?
    算法和数据结构?
    渲染一个react?
    移动端兼容适配?
    PWA全称Progressive Web App,即渐进式WEB应用?
    InnoDB一棵B+树可以存放多少行数据?
    移动端首屏优化?
    InnoDB什么时候会锁表?
    数组去重,多种方法?
    如何处理异形屏iphone X?
  • 原文地址:https://www.cnblogs.com/fanbi/p/10966571.html
Copyright © 2020-2023  润新知