← All notes

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

Sources

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

Permanent ID: iil:note:k8msqay8

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.},
}

Markdown

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.

All artifact files (JSON index)

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.