Go Module 8
Coming from Python or Java interview questions
Interview questions on coming from Python or Java: short warm-ups, then what each harder question tests, the first step of a good answer and its follow-ups.
Warm-up questions
Short questions with short answers, each answered in full.
-
Fresher
What replaces try, except and finally in Go?
Answered in the lesson Go for Python developers: a translation guide
-
Fresher
What replaces Java's Optional as a return type in Go?
Answered in the lesson Go for Java developers: a translation guide
-
Fresher
Why does Go name a getter Owner rather than GetOwner?
Answered in the lesson Idiomatic Go: naming, comments and style guides
Questions with model answers
What each question tests, the first step of a good answer and the follow-ups to expect.
-
Fresher to mid
A Python job reads a large log with a generator, read_rows(path), that opens the file, yields parsed rows and closes the file in a finally block. The caller stops after the first 100 rows that match. Port it to Go: how does the Go version stop reading early, and where is the file closed?
- What it tests
- Writing an iterator over a file with iter.Seq2, what yield returning false means, closing with defer inside the iterator, reporting read errors, and how a push iterator differs from a Python generator.
- First step
- Write rows as a function that returns iter.Seq2[Row, error]. Inside it, open the file, defer its Close and scan line by line, calling yield after each parsed row and returning as soon as yield returns false. The caller's loop breaks at the hundredth match, which makes yield return false, so the iterator returns and its deferred Close runs before the loop statement finishes.
- Follow-ups
-
- How do you report an error that happens halfway through the file?
- What happens if the iterator ignores yield's result and keeps calling it?
- How would you test that the file is closed after an early break?
From the lesson Go for Python developers: a translation guide
Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.Opening the model answers with your pass…
Model answers open with any MySmartCoPilot pass.
- Pro pass · 1 week₹79$2.99
- 30 days₹179$5.99
- 1 year₹1,699$45
One payment; nothing renews unless you choose to. The price shown is the total you pay: no fees or charges are added at checkout.
Passes can’t be bought right now, so the model answers can’t be opened at the moment. If you have a pass, enter your licence key.
It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).
In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A CPU-bound Python report runs on a ThreadPoolExecutor of 8 threads and is barely faster than with one thread. After a port to Go with one goroutine per customer on an 8-core server, it is about 6 times faster, but its grand total is sometimes wrong. Explain both observations, and fix the Go version.
- What it tests
- Knowing what the GIL serialises, why goroutines run in parallel, estimating a speed-up with Amdahl's law, spotting a data race on a shared total, finding it with the race detector, and choosing a fix.
- First step
- Explain the Python result first: in the default CPython build the global interpreter lock lets one thread run bytecode at a time, so eight threads that only compute take turns. Then look at how the Go version adds to the total. If every goroutine runs total += sum on one shared variable, the updates race and some are lost; running the job with -race confirms it.
- Follow-ups
-
- Why was the Go version about 6 times faster and not 8?
- Which fix do you choose, a mutex, an atomic or one result per goroutine?
- Would Python's build without the GIL change the Python numbers?
From the lesson Go for Python developers: a translation guide
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A Java OrderService gets its repository and notifier through @Autowired fields, and its tests start the whole Spring context with an embedded database. Port it to Go: how do you wire the dependencies without a container, where do the interfaces live, and how do you test the service without a database?
- What it tests
- Constructor injection in main, deciding where the interfaces belong and how much each holds, what constructors return, fakes in table tests, and keeping configuration and start-up order explicit.
- First step
- Write down what OrderService needs from each dependency, method by method, and where each dependency is created today. In Go nothing creates them behind the service's back, so decide which function builds them, how they reach the service, and what a test would hand the service in their place.
- Follow-ups
-
- The service needs ten dependencies. Does main become unreadable?
- Where do settings such as the SMS gateway's address come from?
- How do you stop a fake from drifting away from the real repository?
From the lesson Go for Java developers: a translation guide
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
Java code sums areas with double total(List<? extends Shape> shapes), and callers pass a List<Circle>. In Go, passing a []Circle to func Total(shapes []Shape) float64 does not compile. Why not, what are your options, and which would you choose for a library function?
- What it tests
- Knowing that Go slices have no variance and why, writing a generic function with a constraint, the cost of copying into a []Shape, and keeping a library's API compatible.
- First step
- State exactly what the compiler refuses: a []Circle and a []Shape are different types, and Go converts neither into the other, although each Circle satisfies Shape. Then list what a caller or the function could do instead, and judge each option by its cost and by what it changes for the callers of a library.
- Follow-ups
-
- What does the generic version cost compared with the interface version?
- When would you keep []Shape in the API anyway?
- How do you write Java's <? super T> case in Go?
From the lesson Go for Java developers: a translation guide
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A Java service calls a slow partner API from Executors.newFixedThreadPool(200), so that it never sends more than 200 requests at once, and it cancels the calls when a request times out. The Go port starts one goroutine per call. What must you keep from the Java design, and how do you write it in Go?
- What it tests
- Seeing that the pool was a limit on concurrency, bounding goroutines with a semaphore, carrying cancellation with a context, collecting results and errors, and avoiding goroutine leaks.
- First step
- Name what the pool did besides running tasks: it capped the calls to the partner at 200. Decide how the Go version keeps that cap when every call has a goroutine of its own, and what must happen to the calls still in flight when the request that started them times out.
- Follow-ups
-
- Why not keep 200 long-lived worker goroutines?
- How do you return the first error and stop the other calls?
- How would you find goroutines that leaked after a timeout?
From the lesson Go for Java developers: a translation guide
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
A package named utils exports UtilsFormatDate, UtilsParseAmount and a type UtilsCache, and 40 files across the code base import it. How would you rename these idiomatically, and how do you move the 40 files over without one huge change?
- What it tests
- Package names and stutter, splitting a catch-all package by what it provides, and moving callers in steps with forwarding functions, type aliases, Deprecated notices and go fix.
- First step
- Group the exported names by what they do: dates, amounts and caching. Name a package for each job so that its names read well after it, such as datefmt.Format and amount.Parse, with nothing repeated. Then plan how the 40 files keep compiling while they move, a few at a time.
- Follow-ups
-
- How does a type alias help here, and what can it not do?
- How do you stop new code from using the old names?
- When is it better to leave a badly named package alone?
From the lesson Idiomatic Go: naming, comments and style guides
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
Your library's page on pkg.go.dev shows half-sentences as code blocks and a list of rules that has lost its structure, and a function you deprecated a year ago still looks current. What went wrong in the doc comments, and how do you fix them and keep them right?
- What it tests
- The doc comment syntax: indented lines as code blocks, flat lists, the Deprecated paragraph and doc links, and checking comments with gofmt and go doc before a release.
- First step
- Open the source of one broken comment and compare its indentation with what the page shows: in a doc comment, an indented line that is not a list item is a code block. Then compare the structure of the lists in the source with the page, and look at how the deprecated function's comment says that it is deprecated.
- Follow-ups
-
- How do you check the comments before a release, without publishing?
- What does the Deprecated paragraph change for the library's users?
- How do you link to a function of another package from a comment?
From the lesson Idiomatic Go: naming, comments and style guides
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Senior to staff
Your team of twelve is adopting Go. One senior engineer wants the Uber Go Style Guide, another the Google Go Style Guide, and code reviews already argue about underscores and line length. How do you decide, what do you enforce with tools, and what do you leave to reviews?
- What it tests
- Knowing what each document is and how binding it is, where the guides disagree, turning decisions into tool settings, and keeping reviews for design rather than style.
- First step
- Start from what is not up for debate: gofmt on every file, go vet in every build, and the Go project's own documents, Effective Go read beside the release notes, and the Code Review Comments wiki. Then list the points where the guides differ, such as underscores in names and a line-length limit, and decide each one once, in writing.
- Follow-ups
-
- Which rules can tools enforce, and which can they not?
- What do you do with existing code that breaks the new rules?
- May you copy the chosen guide into your team's wiki?
From the lesson Idiomatic Go: naming, comments and style guides
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes