package main import ( "net" ) func main() { conn, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 5000}) if err != nil { panic(err) } defer conn.Close() data := []byte("Hello, World!") addr := &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 6000} _, err = conn.WriteToUDP(data, addr) if err != nil { panic(err) } }
package main import ( "net" ) func main() { conn, err := net.DialUDP("udp", nil, &net.UDPAddr{IP: net.ParseIP("8.8.8.8"), Port: 53}) if err != nil { panic(err) } defer conn.Close() dnsQuery := []byte{0x66, 0x6f, 0x6f, 0x03, 0x62, 0x61, 0x72, 0x00, 0x00, 0x01, 0x00, 0x01} _, err = conn.WriteToUDP(dnsQuery, conn.RemoteAddr()) if err != nil { panic(err) } }This example creates a UDP connection to the DNS server at `8.8.8.8:53` and sends a DNS query for the "foo.bar" domain. Overall, the `net` package in Go provides a rich set of networking functions for building network applications.