-2

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 < vof.Field(0).Len(); i++ {
            // how to know this field is []int or []string?
            // vof.Field(0).Index(i).Set()
        }
    default:
        return
    }
}
AM031447
  • 475
  • 1
  • 8
  • 21
  • 3
    Use `vof.Field(0).Type().Elem()` to get the [`reflect.Type`](https://pkg.go.dev/reflect@go1.19#Type) representation of the slice element type. You can then use `Kind` on that `reflect.Type`. – mkopriva Aug 17 '22 at 08:33
  • 1
    @mkopriva got it , the data type of the slice was successfully obtained , thx! – AM031447 Aug 17 '22 at 08:39

3 Answers3

2

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("empty slice")
            return
        }
        switch vof.Field(0).Index(0).Kind() {
        case reflect.Int:
            fmt.Println("int here")
            // for i := 0; i < 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("string here")
        }
    default:
        return
    }
}

playground

Rahmat Fathoni
  • 1,272
  • 1
  • 2
  • 8
1

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 "[]int":
            fmt.Println("int here")
        case "[]string":
            fmt.Println("string here")
        }
    default:
        return
    }
}
NoahFan
  • 86
  • 4
-2

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.&text=Parameters%3A%20This%20function%20takes%20only,of%20Type%20type(t).

  • but in my case , will get []reflect.Value – AM031447 Aug 17 '22 at 08:35
  • please dont just send a link to the answer. Write the relevant portions here such that if in the future that site goes down, the plain text is still here. You can keep the link as a reference, but you need actual content first. – tritium_3 Aug 21 '22 at 16:18
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 21 '22 at 16:18