Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Appending slices unreliable behaviour. #29710

Closed
Armatorix opened this issue Jan 12, 2019 · 7 comments
Closed

Appending slices unreliable behaviour. #29710

Armatorix opened this issue Jan 12, 2019 · 7 comments

Comments

@Armatorix
Copy link

Armatorix commented Jan 12, 2019

What version of Go are you using (go version)?

1.11.1

What did you do?

I've append two slices.
https://play.golang.org/p/SV9LKgUV496

What did you expect to see?

ID: [0 1 2 3 4 5 6 7 8 9]
IDX: [0 1 0 1 2 3 4 5 6 7 8 9]
ID: [0 1 5 6 7 8 9 7 8 9]
IDX: [0 1 5 6 7 8 9]

What did you see instead?

ID: [0 1 2 3 4 5 6 7 8 9]
IDX: [0 1 0 1 2 3 4 5 6 7 8 9]
ID: [0 1 2 3 4 5 6 7 8 9]
IDX: [0 1 5 6 7 8 9]

what is the purpose?

So IMO this function is not reliable, because when there is enough space in the allocated memory we're changing first arguments value, and if there is not enough we're not doing it. So we're ending with a various result of this function.
Second thing is if I've got return statement with result slice, I'm not expecting to change arguments values.

@tomor
Copy link

tomor commented Jan 12, 2019

IMO the method behaves exactly as described in the docs: https://golang.org/pkg/builtin/#append

@ianlancetaylor
Copy link
Contributor

Please read https://blog.golang.org/slices .

@Armatorix
Copy link
Author

Yes, it is working as described. But I'm wondering if it is a good approach. IMO append loses its semantic meaning when working like this.

@ianlancetaylor
Copy link
Contributor

Slices work as they are documented. That is not something we are going to change at this point in the language's development.

@Armatorix
Copy link
Author

Armatorix commented Jan 17, 2019

If something is documented it doesn't imply that it is good. I can document that my function is doing unexpected things and implement it like so, and that would not be good at all.
So you're telling that when you would see for first time function append() which returns some slice, you're expecting that in some situations it changes it arguments values and in some not?

@ianlancetaylor
Copy link
Contributor

The language acts as is documented and intended. I understand that you find it surprising, but there are good reasons for it to act this way, and more importantly, as I said, this is not something that can be changed at this point in the language's development. If you need to discuss this further, please use a forum; see https://golang.org/wiki/Questions .

@Armatorix
Copy link
Author

Thx for the link, it will be probably better to use other discussion channel.

@golang golang locked and limited conversation to collaborators Jan 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants