Standard library / net
net.Listen()
Bind a local TCP listener; port zero requests an available port. Close when finished. Expected stdout: 'tcp\n'.
Formal Syntax & Structure
go syntaxgo
func Listen(network, address string) (Listener, error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| network | string | Network family such as tcp4 or udp4. |
| address | string | Network address including port when required. |
Return Value
(Listener, error)
Operation result; check errors and validity indicators when returned.
Working Example
Example Snippetgo
package main
import ( "fmt" "net")
func main() { l := must(net.Listen("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) }}