复制相同类型的数组

I would like to write a program that receive a array (of string, int, or whatever) and create another array of the same type contain only the first element.

For example:

for a array of strings arr := []string("hello", "world")

my output would be arr2 := []string(arr[0]);

I can't use the copy function because to do that, i would have to create(make) a new slice for it. And in this case, i still have to discover which type the first array is (string, int, bool, and so on...)

Maybe I could use the reflect.TypeOf() but i would still not know how to use that information to create the same type of slice or array.

i'm not considering to use conditionals for that. For example:

if reflect.TypeOf(arr) == []int {
   arr := []int(arr[0])
} else if reflect.TypeOf(arr) == []string
   arr := []string(arr[0])
} ...

I would be glad get a help on there. Thanks in advance.

You could just subslice it in place:

s2 := s1[0:1]

But if you really need to create a new slice, you can do it like this:

func f(s interface{}) interface{} {
    v := reflect.ValueOf(s)
    t := v.Type()
    res := reflect.MakeSlice(t, 1, 1)
    res.Index(0).Set(v.Index(0))
    return res.Interface()
}

Playground: http://play.golang.org/p/w1N3pgvAwr.