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
proposal: net: Addr, LocalAddr, RemoteAddr must return established endpoint addresses #9654
Comments
I think the obvious behaviour would be to give the caller a copy of the Addr. After looking at the code I think the only way to do this is to give Addr the ability to copy itself. However, we can't change the Addr interface, so I think introducing an interface like
might work around the problem. Make all the net.(.*)Addr types implement this function and let LocalAddr() (and probably RemoteAddr() as well) check if the given Addr also implements AddrCopier. If so, return a copy. If not, return the original value, because that's all we got. I know this isn't a real fix, but I think it's the only solution. |
I don't think this could be done: it's still a breaking change. For example, user might use the result from LocalAddr() to index a map And I don't think this issue is a problem at all. Go doesn't have immutable |
Please open a new issue if you have something for the immutability of concrete types that implement net.Addr interface. This issue should focus on the behavior of Addr, LocalAddr and RemteAddr. |
True, except string types.
Like other APIs such as various DNS record lookups, listing nework interfaces and addresses, re-using the returned values from the net package is fine. I think this issue has been overlooked for a long time. |
In Go 2, we probably should make all existing net.Addr implementations implement This is in line with other packages that returns internal state that shouldn't be modified. |
Alright, I think this is reasonable. I don't see an elegant way of doing this without non-pointer receivers. |
Ideally, those methods should return a copy of the Addr, but due to the Go 1 API guarantee, we cannot make that change now: there might exist client code that uses the returned Addr as map index and thus relies on the fact that different invocation of the method returns the same pointer. Changing this behavior will lead to hidden behaviour change in those programs. Update #9654. Change-Id: Iad4235f2ed7789b3a3c8e0993b9718cf0534ea2b Reviewed-on: https://go-review.googlesource.com/3851 Reviewed-by: Mikio Hara <mikioh.mikioh@gmail.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Example here: http://play.golang.org/p/0zfs2UZ7ZY
I don't know if this is intended, but it feels weird.
The text was updated successfully, but these errors were encountered: