Skip to content
tezvyn:

Build a TCP server in Go and Rust using standard libraries

Source: pkg.go.devEasyHow cards are made

Tests standard-library networking APIs in both languages. Strong answer: Go's net.Listen with Accept loop vs Rust's std::net::TcpListener::bind and incoming iterator. Red flag: reaching for HTTP or async frameworks instead of core TCP primitives.

What's really being asked

This question checks whether you can name the exact standard-library networking primitives for a bare-metal TCP server in two languages with different concurrency models. Interviewers want to see that you know the boundary between TCP and HTTP, that you understand the listen-accept-handle lifecycle, and that you do not overcomplicate a blocking synchronous task with async runtimes or external crates.

The full answer

First, in Go, you use the net package. Call net.Listen with the network string tcp and an address like :8080, which returns a net.Listener. Then run a loop calling listener.Accept, which returns a net.Conn and an error. Each connection is usually handled in its own goroutine so the server can accept concurrently. Second, in Rust, you use std::net. Call std::net::TcpListener::bind with an address string, then iterate over listener.incoming or call accept in a loop, which yields a std::net::TcpStream. Because this is the standard library, both approaches are blocking; the key difference is that Go pairs the accept loop with lightweight goroutines while Rust would typically move each TcpStream into a dedicated thread for concurrent handling. Third, mention that you must close or drop connections to avoid leaking file descriptors.

The mistakes people make

Naming net/http or the http crate shows you are thinking at the wrong layer of abstraction. Proposing Tokio, async-std, or hyper is a red flag because the question explicitly asks for the standard library in both languages. In Rust, confusing std::net::TcpListener with std::os::unix::net::UnixListener is another miss. In Go, forgetting to mention that Accept blocks and returns a Conn is a gap.

What usually comes next

How would you limit the number of concurrent connections? How would you gracefully shut down the server? What is the difference between TcpListener::bind in Rust and net.Listen in Go regarding default address formats? How would you add TLS without changing the overall structure?

A concrete example

In Go: listener, err := net.Listen("tcp", ":8080"); for { conn, err := listener.Accept(); go handleConn(conn) }. In Rust: let listener = std::net::TcpListener::bind("0.0.0.0:8080")?; for stream in listener.incoming() { let stream = stream?; std::thread::spawn(move || handle(stream)); }.

Interview question

When implementing a synchronous TCP server using only standard libraries, how do Go and Rust differ in handling multiple concurrent connections?

  • a.Go uses net.Listen with goroutines, while Rust uses std::os::unix::net::UnixListener with dedicated threads
  • b.Go uses net/http with goroutines, while Rust uses std::net::TcpListener with blocking iterators only
  • c.Go spawns a goroutine for each accepted net.Conn, while Rust typically moves each std::net::TcpStream into a dedicated threadCorrect
  • d.Both use blocking accept loops but require external crates like Tokio to process connections concurrently
Why?

The card states that Go pairs listener.Accept with goroutines for concurrent handling, while Rust moves each TcpStream into a dedicated thread when using only the standard library. Option B is tempting because Rust's std::net is indeed blocking, but it wrongly places Go at the HTTP layer and incorrectly suggests Rust cannot spawn threads for concurrency.

Just read this? Test yourself on what you have been reading.

Read the original → pkg.go.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on go — each one lists the topics its interview covers.

See open roles