Standard library / net

net.ListenConfig

Configure listener creation; context cancellation controls setup, not the lifetime of the returned listener. Expected stdout: 'tcp\n'.

Formal Syntax & Structure

go syntaxgo
type ListenConfig struct {  Control func(network, address string, c syscall.RawConn) error
  KeepAlive time.Duration
  KeepAliveConfig KeepAliveConfig
}

Working Example

Example Snippetgo
package main
import (  "context"  "fmt"  "net")
func main() {  lc := net.ListenConfig{}  l := must(lc.Listen(context.Background(), "tcp4", "127.0.0.1:0"))  defer l.Close()  fmt.Println(l.Addr().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