func (c *UDPConn) Read(b []byte) (int, error)
package main import ( "fmt" "net" ) func main() { addr := &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 9000} conn, _ := net.ListenUDP("udp", addr) defer conn.Close() buffer := make([]byte, 1024) n, addr, _ := conn.ReadFromUDP(buffer) fmt.Println("Received data:", string(buffer[:n]), "from", addr.String()) }
package main import ( "fmt" "net" ) func main() { addr := &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 9000} conn, _ := net.DialUDP("udp", nil, addr) defer conn.Close() message := []byte("Hello, world!") conn.Write(message) buffer := make([]byte, 1024) n, _ := conn.Read(buffer) fmt.Println("Server response:", string(buffer[:n])) }This example creates a UDP client that sends a "Hello, world!" message to a server running on port 9000. The server responds with a message, which is printed to the console. Both of these examples use the `net` package to work with UDP connections.