创建根据输入slice参数执行不同操作的函数

I just started to learn Go language and I want to build a function which will be selecting a random subsequence from a slice. However, I don't know what type of values this slice can store, these can be integers, strings or elements of some struct. For example, let's assume I have to structures:

type person struct {
    name string
    age  int
}

type animal struct {
    name  string
    age   int
    breed string
}

Now, I want to build function getRandomSequence as follows: given as arguments a slice S and a length l the function returns a slice which contains l randomly selected elements from slice S. The problem which I encountered was - how to make this function work for any possible slice. I tried to do the following:

func GetRandomSequence(S interface{}, l int) []interface{} {
   switch S.(type) {
   case person:
      // Do random selection of l elements from S and return them
   case animal:
      // Do random selection of l elements from S and return them
   case int:
     // Do random selection of l elements from S and return them
   }
   return " Not Recognised"
}

Can someone suggest how I can write such function? I manage to make similar (i.e., general) functions work if S would be a single element of any type (so instead of []interface{} would be just interface{}) but I cannot find out how to solve this problem.

Just use interface{} not []interface{}. An empty interface can store any type, including slices.

Your code would look something like this (although I didn't test):

func GetRandomSequence(S interface{}, l int) interface{} {
   returnSlice := []interface{}
   switch v := s.(type) {
   // inside the switch v has the value of S converted to the type
   case []person:
      // v is a slice of persons here
   case []animal:
      // v is a slice of animals here
   case []int:
      // v is a slice of ints here
   case default:
       // v is of type interface{} because i didn't match any type on the switch
       // I recommend you return nil on error instead of a string
       // or always return 2 things, the value and an error like 
       // the standard library
       return "Not Recognized" 
   }
   rerurn returnSlice
}

I recommend you do the complete Tour of go, but for this question the answer is here.

Depending on what you want to do exactly, it looks like you might not need different types of slices but a slice of interface{}. If in your function to extract random elements from the slice you don't care about the type of the elements just do:

func GetRandomSequence(S []interface{}, l int) []interface{} {
    returnSlice := make([]interface{}, 0, l)
    for i:=0; i<l; i++ { 
        // S[i] here is always of type interface{}
        returnSlice = append(returnSlice, S[getRnd()]) // you need to implement getRnd() or just "math/rand" or something.
    }
    return returnSlice 
}

Write a sample function that works with slice indices.

// Sample k random elements from set of n elements.
// The function set sets an element in the output given 
// an index in the output and the index in the input.  
func sample(k int, n int, assign func(out int, in int)) {
    for i := 0; i < k; i++ {
        set(i, i)
    }
    for i := k; i < n; i++ {
        j := rand.Intn(i + 1)
        if j < k {
            set(j, i)
        }
    }
}

Use it like this:

in := []person{ {"John", 10}, {"Sally", 11}, {"James", 9}, {"Eve", 8} }
out := make([]person, 2)
sample(len(out), len(in), func(i, j int) { out[i] = in[j] })

Because sample works with length and index values only, it can be used on a slice of any type.

This approach is similar to sort.Search in the standard library.