github.com/slayercat/go@v0.0.0-20170428012452-c51559813f61/src/net/example_test.go (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package net_test 6 7 import ( 8 "fmt" 9 "io" 10 "log" 11 "net" 12 ) 13 14 func ExampleListener() { 15 // Listen on TCP port 2000 on all interfaces. 16 l, err := net.Listen("tcp", ":2000") 17 if err != nil { 18 log.Fatal(err) 19 } 20 defer l.Close() 21 for { 22 // Wait for a connection. 23 conn, err := l.Accept() 24 if err != nil { 25 log.Fatal(err) 26 } 27 // Handle the connection in a new goroutine. 28 // The loop then returns to accepting, so that 29 // multiple connections may be served concurrently. 30 go func(c net.Conn) { 31 // Echo all incoming data. 32 io.Copy(c, c) 33 // Shut down the connection. 34 c.Close() 35 }(conn) 36 } 37 } 38 39 func ExampleCIDRMask() { 40 // This mask corresponds to a /31 subnet for IPv4. 41 fmt.Println(net.CIDRMask(31, 32)) 42 43 // This mask corresponds to a /64 subnet for IPv6. 44 fmt.Println(net.CIDRMask(64, 128)) 45 46 // Output: 47 // fffffffe 48 // ffffffffffffffff0000000000000000 49 }