Dial or listen for TCP in Go with the net package
Answer: Package net provides a portable interface for network I/O, including TCP/IP also, UDP, domain name resolution, and Unix domain sockets. Most clients will need only the basic interface provided by the Dial, Listen, and Accept also functions and the associated Conn and Listener interfaces.
Dial
The Dial function connects to a server. On error, handle the error before using the connection. The crypto/tls package uses the same interfaces and similar Dial and Listen functions.
Listen
The Listen function creates servers. A loop can call Accept, then handle the Conn. Name resolution may happen with Dial or directly with LookupHost and LookupAddr. On Unix systems the resolver can use a pure Go resolver or a cgo-based resolver. A blocked DNS request in the pure Go resolver consumes only a goroutine.
| Dial | Listen | |---|---| | Dial | Connects to a server and returns a Conn | | Listen | Creates a server Listener | | Accept | Accepts the next incoming Conn | | Conn | Interface for a network connection | | Listener | Interface returned by Listen |
See also
Claims
- id: C1 claim: The net package provides a portable interface for TCP, UDP, DNS, and Unix sockets. source: S1 evidence: "portable interface for network I/O, including TCP/IP, UDP"
- id: C2 claim: Most clients need only Dial, Listen, Accept, and the Conn and Listener interfaces. source: S2 evidence: "basic interface provided by the [Dial], [Listen], and Accept functions"
- id: C3 claim: The Listen function creates servers that accept connections. source: S2 evidence: "The Listen function creates servers:"
Sources
- [S1] Package net. Go docs. https://pkg.go.dev/net . License: BSD-3-Clause. Accessed 2026-11-19.
- [S2] net.go package comment. Go source. https://raw.githubusercontent.com/golang/go/master/src/net/net.go . License: BSD-3-Clause. Accessed 2026-11-19.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Go docs, Package net; Go source, net.go package comment. BSD-3-Clause; BSD-3-Clause. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Go docs, Package net; Go source, net.go package comment. BSD-3-Clause; BSD-3-Clause.
Cite this
iinventedlight. 2026. "Dial or listen for TCP in Go with the net package." iinventedlight Library, iil:note:k8msqay8, version of 2026-11-19. https://iinventedlight.com/library/notes/go-net-dial-listen/. License: CC-BY-4.0.
@misc{iil_note_k8msqay8,
author = {{iinventedlight}},
title = {{Dial or listen for TCP in Go with the net package}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-19},
url = {https://iinventedlight.com/library/notes/go-net-dial-listen/},
note = {Permanent ID iil:note:k8msqay8. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- go-net-dial-listen.table.json (table; CC-BY-4.0)
- go-net-dial-listen.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-net-dial-listen.md.