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
Export next and prev fields of a Linked List Element #40406
Comments
Use the |
@ianlancetaylor fair enough, that solves the problem of moving through the list. Now, imagine that you have to remove that Element, without having access to the list. Normally, in the case of a singly linked list, you could copy next Element's value to the current Element's value (this can be done) then you should point current Element's next pointer to Element.Next().Next() and finally put the next Element's next pointer equal to nil (Go's garbage collector should take care of this dangling Element). Since you can't access Element.next, you therefore can't assign Element.Next().Next() to it and you can't remove the next Element's next pointer. To sum it up, deletion of an Element cannot be done in Go if you don't have access to the linked list itself. If next and prev should not be exported, at least a function to remove the Element, to be called on the Element itself, should be made available. |
The container/list package does not support removing an element from a list without having a pointer to the list. We aren't going to change that package to support a different use case. If you need different functionality, I suggest that you write your own package. This is not impossible; the container/list package is less than 250 lines, not counting tests. The standard library package can't be all things to all people; it has to make choices as to what it will support. |
To be honest I don't see how implementing additional stuff, that may help more people without taking away from the rest, can hurt the language itself or make the language library any worse. But I guess I might be missing something and that you have your reasons to state that you won't implement it, so I totally respect that. You are right in saying that it is pretty much straightforward to implement this - by opening this issue I just wanted to see if there was the possibility to have the implementation being absorbed in language itself, so to benefit more people in my same position. Thanks for taking the time to answer. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
darwin/amd64What did you do?
Tried to access an Element's previous and next pointers, with Element being the struct contained in Go's linked list implementation.
What did you expect to see?
Element's prev and next values exported for public use.
What did you see instead?
Element's prev and next are not being exported for usage outside of the package, see:
go/src/container/list/list.go
Line 21 in 8696ae8
Can these two be exported? If you have access to one and only one specific Element of the linked list, and not to the entire list, it is not possible to access previous and next Elements of the linked list; in other words, you are stuck on that single Element you have access to and can't move back or forward through the linked list. If this change gets approved I can also work on a PR to fix it. Thanks.
The text was updated successfully, but these errors were encountered: