]> Cypherpunks repositories - gostls13.git/commitdiff
net: add example for net.UDPConn.WriteTo function
authorAdam Medzinski <adam.medzinski@gmail.com>
Fri, 18 May 2018 14:54:30 +0000 (16:54 +0200)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 23 May 2018 18:24:17 +0000 (18:24 +0000)
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: Brad Fitzpatrick <bradfitz@golang.org>
src/net/example_test.go

index 289d84f7c7155f7d583ae42362da78cef64b7b33..c6eb75d0a4e68f1115c725b844c624993309b15a 100644 (file)
@@ -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)
+       }
+}