In the Go library source you often see that a slice is passed by creating a new slice storage reference like so
method(s[:])
What’s the benefit of this, compared to just passing the original slice?
method(s)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
s[:]construct is normally used only to create a new slice referencing an existing array, not for “passing the original slice”.If
s[:]is really used somewhere in the stdlib andsis a slice than it could be e.g. a refactoring leftover. Please report such place if known to you on the Go issue tracker.