diff --git a/eBook/examples/chapter_15/dial.go b/eBook/examples/chapter_15/dial.go new file mode 100644 index 0000000..432cea0 --- /dev/null +++ b/eBook/examples/chapter_15/dial.go @@ -0,0 +1,24 @@ +// make a connection with www.example.org: +package main + +import ( + "fmt" + "net" + "os" +) + +func main() { + conn, err := net.Dial("tcp", "192.0.32.10:80") // tcp ipv4 + checkConnection(conn, err) + conn, err = net.Dial("udp", "192.0.32.10:80") // udp + checkConnection(conn, err) + conn, err = net.Dial("tcp", "[2620:0:2d0:200::10]:80") // tcp ipv6 + checkConnection(conn, err) +} +func checkConnection(conn net.Conn, err error) { + if err != nil { + fmt.Printf("error %v connecting!") + os.Exit(1) + } + fmt.Println("Connection is made with %v", conn) +}