Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the element type of slice interface{}?

Tags:

go

I have the following code to double the slice.

func doubleSlice(s []int) []int  {
  t := make([]int, len(s), (cap(s) + 1) * 2 )
  for i := range s {
    t[i] = s[i]
  }
  return t
}

I want to make the func to double any type of slice. And I need to know the element type first.

func showInterfaceItem(s interface{}) interface{} {
  if reflect.TypeOf(s).Kind() != reflect.Slice {
    fmt.Println("The interface is not a slice.")
    return
  }

  var t interface{}
  newLen := reflect.ValueOf(s).Len()
  newCap := (cap(reflect.ValueOf(s).Cap()) + 1) * 2
  t = make([]reflect.TypeOf(s), newLen, newCap)

  return t
}

The reflect.TypeOf(s) return the type of interface{}, not the type of element. How can I get the element type of slice interface?

like image 933
firelyu Avatar asked Feb 10 '17 04:02

firelyu


1 Answers

You can use reflect.TypeOf(s).Elem() to get the type of element of slice.

package main

import (
    "fmt"
    "reflect"
)

func doubleSlice(s interface{}) interface{} {
    if reflect.TypeOf(s).Kind() != reflect.Slice {
        fmt.Println("The interface is not a slice.")
        return nil
    }

    v := reflect.ValueOf(s)
    newLen := v.Len()
    newCap := (v.Cap() + 1) * 2
    typ := reflect.TypeOf(s).Elem()

    t := reflect.MakeSlice(reflect.SliceOf(typ), newLen, newCap)
    reflect.Copy(t, v)
    return t.Interface()
}

func main() {
    xs := doubleSlice([]string{"foo", "bar"}).([]string)
    fmt.Println("data =", xs, "len =", len(xs), "cap =", cap(xs))

    ys := doubleSlice([]int{3, 1, 4}).([]int)
    fmt.Println("data =", ys, "len =", len(ys), "cap =", cap(ys))
}

The output will be:

data = [foo bar] len = 2 cap = 6
data = [3 1 4] len = 3 cap = 8

Check it in: Go Playground

like image 116
ymonad Avatar answered Nov 04 '22 01:11

ymonad