Standard library / net

net.ListenMulticastUDP()

Join a UDP multicast group on a chosen interface. A port of zero selects an available port. Go disables multicast loopback on this socket; membership does not prove delivery. This example joins on Linux loopback without sending traffic. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func ListenMulticastUDP(network string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error)

Parameters & Attributes

NameTypeDescription
networkstringNetwork family such as tcp4 or udp4.
ifi*InterfaceArgument of the declared type; see example and description.
gaddr*UDPAddrArgument of the declared type; see example and description.

Return Value

(*UDPConn, error)

Operation result; check errors and validity indicators when returned.

Working Example

Example Snippetgo
package main
import (  "fmt"  "net")
func main() {  i := must(net.InterfaceByName("lo"))  c := must(net.ListenMulticastUDP("udp4", i, &net.UDPAddr{IP: net.IPv4(239, 255, 42, 99)}))  defer c.Close()  fmt.Println(c.LocalAddr().(*net.UDPAddr).Port > 0)}
func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference