Commit 645d4726 authored by Adam Medzinski's avatar Adam Medzinski Committed by Brad Fitzpatrick

net: add example for net.UDPConn.WriteTo function

The current documentation of the WriteTo function is very poor and it
is difficult to deduce how to use it correctly. A good example will
make things much easier.

Fixes #25456

Change-Id: Ibf0c0e153afae8f3e0d7d765d0dc9bcbfd69bfb1
Reviewed-on: https://go-review.googlesource.com/113775
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 7ba1c91d
......@@ -119,3 +119,23 @@ func ExampleIPv4Mask() {
// Output:
// ffffff00
}
func ExampleUDPConn_WriteTo() {
// Create connection in non-pre-connected state
conn, err := net.ListenPacket("udp", ":0")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
dst, err := net.ResolveIPAddr("udp", "192.0.2.1:2000")
if err != nil {
log.Fatal(err)
}
// Write data to the desired address
_, err = conn.WriteTo([]byte("data"), dst)
if err != nil {
log.Fatal(err)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment