Skip to content

Commit 2f04903

Browse files
tomocyodeke-em
authored andcommitted
net: document concurrency safety and example for Dialer
Fixes #33743. Change-Id: I80621321d56b6cf312a86e272800f1ad03c5544c GitHub-Last-Rev: d91cb36 GitHub-Pull-Request: #33856 Reviewed-on: https://go-review.googlesource.com/c/go/+/191879 Run-TryBot: Emmanuel Odeke <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Emmanuel Odeke <[email protected]>
1 parent 03ac39c commit 2f04903

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/net/dial.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const (
2323
// The zero value for each field is equivalent to dialing
2424
// without that option. Dialing with the zero value of Dialer
2525
// is therefore equivalent to just calling the Dial function.
26+
//
27+
// It is safe to call Dialer's methods concurrently.
2628
type Dialer struct {
2729
// Timeout is the maximum amount of time a dial will wait for
2830
// a connect to complete. If Deadline is also set, it may fail

src/net/example_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
package net_test
66

77
import (
8+
"context"
89
"fmt"
910
"io"
1011
"log"
1112
"net"
13+
"time"
1214
)
1315

1416
func ExampleListener() {
@@ -37,6 +39,22 @@ func ExampleListener() {
3739
}
3840
}
3941

42+
func ExampleDialer() {
43+
var d net.Dialer
44+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
45+
defer cancel()
46+
47+
conn, err := d.DialContext(ctx, "tcp", "localhost:12345")
48+
if err != nil {
49+
log.Fatalf("Failed to dial: %v", err)
50+
}
51+
defer conn.Close()
52+
53+
if _, err := conn.Write([]byte("Hello, World!")); err != nil {
54+
log.Fatal(err)
55+
}
56+
}
57+
4058
func ExampleIPv4() {
4159
fmt.Println(net.IPv4(8, 8, 8, 8))
4260

0 commit comments

Comments
 (0)