From 645d4726f0f36c3aec9c864f47411a74c20ebc70 Mon Sep 17 00:00:00 2001 From: Adam Medzinski Date: Fri, 18 May 2018 16:54:30 +0200 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- src/net/example_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/net/example_test.go b/src/net/example_test.go index 289d84f7c7..c6eb75d0a4 100644 --- a/src/net/example_test.go +++ b/src/net/example_test.go @@ -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) + } +} -- 2.50.0