Go Learning notes--most combinations and

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.