mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 07:23:28 +08:00
Create dial.go
This commit is contained in:
24
eBook/examples/chapter_15/dial.go
Normal file
24
eBook/examples/chapter_15/dial.go
Normal file
@@ -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)
|
||||||
|
}
|
Reference in New Issue
Block a user