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

Export next and prev fields of a Linked List Element #40406

Closed
itsgarryb opened this issue Jul 25, 2020 · 4 comments
Closed

Export next and prev fields of a Linked List Element #40406

itsgarryb opened this issue Jul 25, 2020 · 4 comments

Comments

@itsgarryb
Copy link

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

$ go version 1.14.6

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env darwin/amd64
$ go env

What 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:

next, prev *Element

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.

@ianlancetaylor
Copy link
Contributor

Use the Next and Prev methods.

@itsgarryb
Copy link
Author

@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.

@ianlancetaylor
Copy link
Contributor

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.

@itsgarryb
Copy link
Author

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.

@golang golang locked and limited conversation to collaborators Jul 26, 2021
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

3 participants