The go language itself does not have a function similar to Array_merge in PHP, and cannot directly implement merging of multiple arrays
But this kind of operation is really common in everyday development,
There are two scenarios to complete this operation
1:append ()
While this function can do the above, using append means traversing the array, meaning that the slice length is dynamically extended
I can only say it's a stupid trick.
2:copy ()
Func copy
Func copy (DST, src []type) int
The copy built-in function copies elements from a source slice into a destination slice. (as a special case, it also would copy bytes from a string to a slice of bytes.) The source and destination may overlap. Copy returns the number of elements copied, which would be the minimum of Len (SRC) and Len (DST).
So be sure to avoid overlapping when using copy
Specific content does not repeat, on the code:
package tooltype CommonFunc struct{}var commonFunc CommonFuncfunc (c *CommonFunc) Merge(s ...[]interface{}) (slice []interface{}) { switch len(s) { case 0: break case 1: slice = s[0] break default: s1 := s[0] s2 := commonFunc.Merge(s[1:]...)//...将数组元素打散 slice = make([]interface{}, len(s1)+len(s2)) copy(slice, s1) copy(slice[len(s1):], s2) break } return}
`
Go Learning notes--most combinations and