Black Lives Matter. Support the Equal Justice Initiative.

Source file src/net/example_test.go

Documentation: net

     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  	"context"
     9  	"fmt"
    10  	"io"
    11  	"log"
    12  	"net"
    13  	"time"
    14  )
    15  
    16  func ExampleListener() {
    17  	// Listen on TCP port 2000 on all available unicast and
    18  	// anycast IP addresses of the local system.
    19  	l, err := net.Listen("tcp", ":2000")
    20  	if err != nil {
    21  		log.Fatal(err)
    22  	}
    23  	defer l.Close()
    24  	for {
    25  		// Wait for a connection.
    26  		conn, err := l.Accept()
    27  		if err != nil {
    28  			log.Fatal(err)
    29  		}
    30  		// Handle the connection in a new goroutine.
    31  		// The loop then returns to accepting, so that
    32  		// multiple connections may be served concurrently.
    33  		go func(c net.Conn) {
    34  			// Echo all incoming data.
    35  			io.Copy(c, c)
    36  			// Shut down the connection.
    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 ExampleIPv4() {
    59  	fmt.Println(net.IPv4(8, 8, 8, 8))
    60  
    61  	// Output:
    62  	// 8.8.8.8
    63  }
    64  
    65  func ExampleParseCIDR() {
    66  	ipv4Addr, ipv4Net, err := net.ParseCIDR("192.0.2.1/24")
    67  	if err != nil {
    68  		log.Fatal(err)
    69  	}
    70  	fmt.Println(ipv4Addr)
    71  	fmt.Println(ipv4Net)
    72  
    73  	ipv6Addr, ipv6Net, err := net.ParseCIDR("2001:db8:a0b:12f0::1/32")
    74  	if err != nil {
    75  		log.Fatal(err)
    76  	}
    77  	fmt.Println(ipv6Addr)
    78  	fmt.Println(ipv6Net)
    79  
    80  	// Output:
    81  	// 192.0.2.1
    82  	// 192.0.2.0/24
    83  	// 2001:db8:a0b:12f0::1
    84  	// 2001:db8::/32
    85  }
    86  
    87  func ExampleParseIP() {
    88  	fmt.Println(net.ParseIP("192.0.2.1"))
    89  	fmt.Println(net.ParseIP("2001:db8::68"))
    90  	fmt.Println(net.ParseIP("192.0.2"))
    91  
    92  	// Output:
    93  	// 192.0.2.1
    94  	// 2001:db8::68
    95  	// <nil>
    96  }
    97  
    98  func ExampleIP_DefaultMask() {
    99  	ip := net.ParseIP("192.0.2.1")
   100  	fmt.Println(ip.DefaultMask())
   101  
   102  	// Output:
   103  	// ffffff00
   104  }
   105  
   106  func ExampleIP_Mask() {
   107  	ipv4Addr := net.ParseIP("192.0.2.1")
   108  	// This mask corresponds to a /24 subnet for IPv4.
   109  	ipv4Mask := net.CIDRMask(24, 32)
   110  	fmt.Println(ipv4Addr.Mask(ipv4Mask))
   111  
   112  	ipv6Addr := net.ParseIP("2001:db8:a0b:12f0::1")
   113  	// This mask corresponds to a /32 subnet for IPv6.
   114  	ipv6Mask := net.CIDRMask(32, 128)
   115  	fmt.Println(ipv6Addr.Mask(ipv6Mask))
   116  
   117  	// Output:
   118  	// 192.0.2.0
   119  	// 2001:db8::
   120  }
   121  
   122  func ExampleCIDRMask() {
   123  	// This mask corresponds to a /31 subnet for IPv4.
   124  	fmt.Println(net.CIDRMask(31, 32))
   125  
   126  	// This mask corresponds to a /64 subnet for IPv6.
   127  	fmt.Println(net.CIDRMask(64, 128))
   128  
   129  	// Output:
   130  	// fffffffe
   131  	// ffffffffffffffff0000000000000000
   132  }
   133  
   134  func ExampleIPv4Mask() {
   135  	fmt.Println(net.IPv4Mask(255, 255, 255, 0))
   136  
   137  	// Output:
   138  	// ffffff00
   139  }
   140  
   141  func ExampleUDPConn_WriteTo() {
   142  	// Unlike Dial, ListenPacket creates a connection without any
   143  	// association with peers.
   144  	conn, err := net.ListenPacket("udp", ":0")
   145  	if err != nil {
   146  		log.Fatal(err)
   147  	}
   148  	defer conn.Close()
   149  
   150  	dst, err := net.ResolveUDPAddr("udp", "192.0.2.1:2000")
   151  	if err != nil {
   152  		log.Fatal(err)
   153  	}
   154  
   155  	// The connection can write data to the desired address.
   156  	_, err = conn.WriteTo([]byte("data"), dst)
   157  	if err != nil {
   158  		log.Fatal(err)
   159  	}
   160  }
   161  

View as plain text