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

doc: add example for net.UDPConn.WriteTo function #25456

Closed
medzin opened this issue May 18, 2018 · 2 comments
Closed

doc: add example for net.UDPConn.WriteTo function #25456

medzin opened this issue May 18, 2018 · 2 comments

Comments

@medzin
Copy link
Contributor

medzin commented May 18, 2018

The current function documentation is rather laconic:

WriteTo implements the PacketConn WriteTo method.

Unfortunately, its use is not as obvious as it may seem because the connection must be in non-pre-connected state (otherwise you get an ErrWriteToConnected error). What does this state really mean and how to achieve it? The first thing I tried was to use UDPConn structure without DialUDP:

udpAddr, resolveErr := net.ResolveUDPAddr("udp4", "localhost:80")
if resolveErr != nil {
	log.Fatalf("Resolve error: %s", resolveErr)
}

udpConn := &net.UDPConn{}
_, writeErr := udpConn.WriteTo([]byte("test"), udpAddr)
if writeErr != nil {
	log.Fatalf("Write error: %s", writeErr)
}

Unfortunately, I've got a very unclear error:

invalid argument

After looking at how everything is implemented, I understood that I need to create a connection using the net.ListenUDP() function, but this is not too intuitive.

An example code of how to use this function (in GoDoc) would significantly simplify its use, without getting deep into the implementation details.

@agnivade
Copy link
Contributor

A PR is most welcome :)

@gopherbot
Copy link

Change https://golang.org/cl/113775 mentions this issue: net: add example for net.UDPConn.WriteTo function

@golang golang locked and limited conversation to collaborators May 23, 2019
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