Skip to Content
Course content

249: Timed Mock Assessment on Core Go

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I've noticed a recurring theme with students reaching this stage of the course. There is a common belief that "if the code compiles and passes the basic test cases, I've mastered the core concepts." It sounds logical, but in Go, that mindset is a dangerous trap. Go's syntax is intentionally sparse, which tricks you into thinking the language is simple. But the real complexity isn't in the syntax—it's in the runtime behavior.

The "It Compiles, So It Works" Trap

Let's look at a classic example involving slices. You might write a function to add an element to a list and think you've nailed it because it compiles and works 90% of the time:

type Manager struct {
    Items []string
}

func (m *Manager) AddItem(item string) {
    m.Items = append(m.Items, item)
}

On the surface, this is fine. But if you don't understand the slice header—the internal structure containing the pointer to the underlying array, the length, and the capacity—you'll be blindsided when you try to share that slice across goroutines or pass it into functions that expect the original slice to be updated without a pointer. I've seen senior devs from other languages spend hours debugging "missing" data simply because they treated a Go slice like a Java ArrayList. They forgot that append might allocate a entirely new underlying array, leaving the old reference pointing to stale data.

What This Mock Assessment is Actually Testing

This isn't a syntax quiz. I'm not interested in whether you remember if a map is declared with make or a literal. I'm testing your mental model of how Go manages memory and concurrency. When you hit the timer on this assessment, I want you to focus on three specific things:

  • Ownership: Who owns this data? Is it being mutated in a way that will cause a race condition? If you're using a channel, are you the one responsible for closing it, or will that cause a panic?
  • Escape Analysis: Is this variable staying on the stack, or is it escaping to the heap? While you don't need to be a compiler engineer, you should be aware when you're creating unnecessary pressure on the Garbage Collector.
  • Interface Satisfaction: Are you over-engineering your interfaces? Remember the Go proverb: "Accept interfaces, return structs." If you find yourself creating a 15-method interface just to satisfy a single function, you're doing it wrong.

You're about to go into a timed environment. My advice? Don't rush to type. Spend the first two minutes of every problem sketching out the data flow in your head. In Go, the most expensive mistakes are the ones that compile perfectly but deadlock your production server at 3 AM.




📋 Practical Task

Concurrent Job Pipeline with Graceful Timeout

Build a small system that simulates a job processor. You need to implement a Dispatcher that manages a pool of workers. The requirements are specific to test your handle on channels, wait groups, and context:

  • The Job: Create a Job struct that contains an ID and a simulated processing time (duration).
  • The Worker: Implement a worker function that consumes jobs from a channel. It must respect a context.Context; if the context is cancelled or times out, the worker must stop processing immediately and exit.
  • The Dispatcher: Create a function that takes a slice of jobs and a maximum number of concurrent workers. It should distribute the jobs and return a slice of "Results" (Job ID and whether it succeeded or timed out).
  • The Constraint: You must ensure that no goroutines are leaked. Use a sync.WaitGroup to ensure the dispatcher only returns once all workers have fully shut down.

Test Case: Pass 10 jobs into the dispatcher with a 3-worker limit. Set a global timeout of 500ms. Some jobs should take 100ms (success) and some should take 1s (timeout). Your final result slice should accurately reflect which jobs finished and which were aborted by the context.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.