package main import ( "fmt" "net" ) func main() { conn, err := net.ListenUDP("udp", &net.UDPAddr{Port: 8080}) if err != nil { panic(err) } defer conn.Close() for { buffer := make([]byte, 1024) _, remoteAddr, err := conn.ReadFromUDP(buffer) if err != nil { fmt.Println("Error reading UDP message:", err) continue } fmt.Printf("Received from %v: %s\n", remoteAddr, string(buffer)) } }
package main import ( "net" ) func main() { conn, err := net.DialUDP("udp", nil, &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8080}) if err != nil { panic(err) } defer conn.Close() message := []byte("Hello, server!") _, err = conn.Write(message) if err != nil { panic(err) } }This example creates a UDP client and sends a message to a server listening on IP address 127.0.0.1 and port 8080. The package library for these examples is "net".