如何使用反射了解切片的数据类型?

huangapple go评论94阅读模式
英文:

how to use reflect know the slice data type?

问题

我想填充切片数据,但是使用reflect.kind()只能告诉我字段(0)是切片,但我不知道它是哪种类型的切片,可能是int[]、string[]或其他类型的切片。

在我知道切片数据类型之前,匆忙设置值会导致恐慌,有人知道如何获取切片类型信息吗?

func WhatSlice(isAny any) {
	vof := reflect.ValueOf(isAny)
	if vof.Kind() != reflect.Struct {
		return
	}
	switch vof.Field(0).Kind() {
	case reflect.Slice:
		for i := 0; i < vof.Field(0).Len(); i++ {
			// 如何知道这个字段是[]int还是[]string?
			// vof.Field(0).Index(i).Set()
		}
	default:
		return
	}
}
英文:

i want to fill the slice data , but using reflect.kind() only let me know field(0) is slice ,
but i don't know it is which type of slice , it culd be int[] or string[] or other type slice

befor i know what slice data type , hastily set value will panic , do some know how to get slice type info?

func WhatSlice(isAny any) {
	vof := reflect.ValueOf(isAny)
	if vof.Kind() != reflect.Struct {
		return
	}
	switch vof.Field(0).Kind() {
	case reflect.Slice:
		for i := 0; i &lt; vof.Field(0).Len(); i++ {
			// how to know this field is []int or []string?
			// vof.Field(0).Index(i).Set()
		}
	default:
		return
	}
}

答案1

得分: 2

你可以在调用循环之前使用vof.Field(0).Index(0).Kind()来检查它。

func WhatSlice(isAny any) {
	vof := reflect.ValueOf(isAny)
	if vof.Kind() != reflect.Struct {
		return
	}
	switch vof.Field(0).Kind() {
	case reflect.Slice:
		if vof.Field(0).Len() == 0 {
			fmt.Println("空切片")
			return
		}
		switch vof.Field(0).Index(0).Kind() {
		case reflect.Int:
			fmt.Println("这里是int")
			// for i := 0; i < vof.Field(0).Len(); i++ {
			// 如何知道这个字段是 []int 还是 []string?
			// vof.Field(0).Index(i).Set()
			// }
		case reflect.String:
			fmt.Println("这里是string")
		}
	default:
		return
	}
}

playground

英文:

You can check it before call for loop with vof.Field(0).Index(0).Kind()

func WhatSlice(isAny any) {
	vof := reflect.ValueOf(isAny)
	if vof.Kind() != reflect.Struct {
		return
	}
	switch vof.Field(0).Kind() {
	case reflect.Slice:
		if vof.Field(0).Len() == 0 {
			fmt.Println(&quot;empty slice&quot;)
			return
		}
		switch vof.Field(0).Index(0).Kind() {
		case reflect.Int:
			fmt.Println(&quot;int here&quot;)
			// for i := 0; i &lt; vof.Field(0).Len(); i++ {
			// how to know this field is []int or []string?
			// vof.Field(0).Index(i).Set()
			// }
		case reflect.String:
			fmt.Println(&quot;string here&quot;)
		}
	default:
		return
	}
}

playground

答案2

得分: 1

使用Type可以直接获取切片类型,但应使用字符串类型来区分它。

func WhatSlice(isAny any) {
	vof := reflect.ValueOf(isAny)

	if vof.Kind() != reflect.Struct {
		return
	}

	switch vof.Field(0).Kind() {
	case reflect.Slice:
		switch vof.Field(0).Type().String() {
		case "[]int":
			fmt.Println("这里是int类型")
		case "[]string":
			fmt.Println("这里是string类型")
		}
	default:
		return
	}
}
英文:

Use the Type you can directly get the slice type
but should use the string type to distinguish it.

func WhatSlice(isAny any) {
	vof := reflect.ValueOf(isAny)

	if vof.Kind() != reflect.Struct {
		return
	}

	switch vof.Field(0).Kind() {
	case reflect.Slice:
		switch vof.Field(0).Type().String() {
		case &quot;[]int&quot;:
			fmt.Println(&quot;int here&quot;)
		case &quot;[]string&quot;:
			fmt.Println(&quot;string here&quot;)
		}
	default:
		return
	}
}

答案3

得分: -2

如果你想获取切片的类型,你可以参考这个页面:

https://www.geeksforgeeks.org/reflect-sliceof-function-in-golang-with-examples/#:~:text=SliceOf()%20Function%20in%20Golang%20is%20used%20to%20get%20the,reflect%20package%20in%20the%20program.&amp;text=Parameters%3A%20This%20function%20takes%20only,of%20Type%20type(t)

英文:

if you want get the type to slice you can refer to this page :

https://www.geeksforgeeks.org/reflect-sliceof-function-in-golang-with-examples/#:~:text=SliceOf()%20Function%20in%20Golang%20is%20used%20to%20get%20the,reflect%20package%20in%20the%20program.&amp;text=Parameters%3A%20This%20function%20takes%20only,of%20Type%20type(t).

huangapple
  • 本文由 发表于 2022年8月17日 15:57:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/73384785.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定