Standard library / net

net.ListenPacket()

Bind a local datagram socket; close after use. Expected stdout: 'udp\n'.

Formal Syntax & Structure

go syntaxgo
func ListenPacket(network, address string) (PacketConn, error)

Parameters & Attributes

NameTypeDescription
networkstringNetwork family such as tcp4 or udp4.
addressstringNetwork address including port when required.

Return Value

(PacketConn, error)

Operation result; check errors and validity indicators when returned.

Working Example

Example Snippetgo
package main
import (  "fmt"  "net")
func main() {  c := must(net.ListenPacket("udp4", "127.0.0.1:0"))  defer c.Close()  fmt.Println(c.LocalAddr().Network())}
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