遍歷切片slice,結構體struct,映射map,interface{}的屬性和值

1 前言code

說明:interface{}必須是前三者類型blog

2 代碼ip

/**
 * @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\n", 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\n", 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())
   }
}

//如下是參數爲interface{},和上面四個其它一一對應
func output110(obj  interface{}){
   value := reflect.ValueOf(obj)
   for i := 0; i < value.NumField(); i++ {
      fmt.Printf("Field %d: %v\n", 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\n", 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)

   //參數是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)
   }


}

輸出:string

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
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
相關文章
相關標籤/搜索