慕丝7291255
乘以 for 循环。没有内置任何东西。in := []string{"push", "pop"}n := 10out := make([]string, 0, len(in)*n) // allocate space for the entire resultfor i := 0; i < n; i++ { // for each repetition... out = append(out, in...) // append, append, ....}fmt.Println(out) // prints [push pop push pop push pop push pop push pop push pop push pop push pop push pop push pop]使用反射包编写通用乘法器:// multiply repeats the slice src n times to the slice pointed to by destiny.func multiply(src interface{}, n int, dstp interface{}) { srcv := reflect.ValueOf(src) result := reflect.MakeSlice(srcv.Type(), 0, srcv.Len()*n) for i := 0; i < n; i++ { result = reflect.AppendSlice(result, srcv) } reflect.ValueOf(dstp).Elem().Set(result)}in := []string{"push", "pop"}var out []stringrepeat(in, 10, &out)fmt.Println(out) // prints [push pop push pop push pop push pop push pop push pop push pop push pop push pop push pop]