mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-11 23:08:34 +08:00
25 lines
547 B
Go
25 lines
547 B
Go
// 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!", err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Printf("Connection is made with %v\n", conn)
|
|
}
|