...
Source file
src/net/example_test.go
Documentation: net
1
2
3
4
5 package net_test
6
7 import (
8 "context"
9 "fmt"
10 "io"
11 "log"
12 "net"
13 "time"
14 )
15
16 func ExampleListener() {
17
18
19 l, err := net.Listen("tcp", ":2000")
20 if err != nil {
21 log.Fatal(err)
22 }
23 defer l.Close()
24 for {
25
26 conn, err := l.Accept()
27 if err != nil {
28 log.Fatal(err)
29 }
30
31
32
33 go func(c net.Conn) {
34
35 io.Copy(c, c)
36
37 c.Close()
38 }(conn)
39 }
40 }
41
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
58 func ExampleDialer_unix() {
59
60
61
62
63 var d net.Dialer
64 ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
65 defer cancel()
66
67 d.LocalAddr = nil
68 raddr := net.UnixAddr{Name: "/path/to/unix.sock", Net: "unix"}
69 conn, err := d.DialContext(ctx, "unix", raddr.String())
70 if err != nil {
71 log.Fatalf("Failed to dial: %v", err)
72 }
73 defer conn.Close()
74 if _, err := conn.Write([]byte("Hello, socket!")); err != nil {
75 log.Fatal(err)
76 }
77 }
78
79 func ExampleIPv4() {
80 fmt.Println(net.IPv4(8, 8, 8, 8))
81
82
83
84 }
85
86 func ExampleParseCIDR() {
87 ipv4Addr, ipv4Net, err := net.ParseCIDR("192.0.2.1/24")
88 if err != nil {
89 log.Fatal(err)
90 }
91 fmt.Println(ipv4Addr)
92 fmt.Println(ipv4Net)
93
94 ipv6Addr, ipv6Net, err := net.ParseCIDR("2001:db8:a0b:12f0::1/32")
95 if err != nil {
96 log.Fatal(err)
97 }
98 fmt.Println(ipv6Addr)
99 fmt.Println(ipv6Net)
100
101
102
103
104
105
106 }
107
108 func ExampleParseIP() {
109 fmt.Println(net.ParseIP("192.0.2.1"))
110 fmt.Println(net.ParseIP("2001:db8::68"))
111 fmt.Println(net.ParseIP("192.0.2"))
112
113
114
115
116
117 }
118
119 func ExampleIP_DefaultMask() {
120 ip := net.ParseIP("192.0.2.1")
121 fmt.Println(ip.DefaultMask())
122
123
124
125 }
126
127 func ExampleIP_Mask() {
128 ipv4Addr := net.ParseIP("192.0.2.1")
129
130 ipv4Mask := net.CIDRMask(24, 32)
131 fmt.Println(ipv4Addr.Mask(ipv4Mask))
132
133 ipv6Addr := net.ParseIP("2001:db8:a0b:12f0::1")
134
135 ipv6Mask := net.CIDRMask(32, 128)
136 fmt.Println(ipv6Addr.Mask(ipv6Mask))
137
138
139
140
141 }
142
143 func ExampleCIDRMask() {
144
145 fmt.Println(net.CIDRMask(31, 32))
146
147
148 fmt.Println(net.CIDRMask(64, 128))
149
150
151
152
153 }
154
155 func ExampleIPv4Mask() {
156 fmt.Println(net.IPv4Mask(255, 255, 255, 0))
157
158
159
160 }
161
162 func ExampleUDPConn_WriteTo() {
163
164
165 conn, err := net.ListenPacket("udp", ":0")
166 if err != nil {
167 log.Fatal(err)
168 }
169 defer conn.Close()
170
171 dst, err := net.ResolveUDPAddr("udp", "192.0.2.1:2000")
172 if err != nil {
173 log.Fatal(err)
174 }
175
176
177 _, err = conn.WriteTo([]byte("data"), dst)
178 if err != nil {
179 log.Fatal(err)
180 }
181 }
182
View as plain text