Skip to content
Learn Netverks

Lesson

Step 19/36 53% through track

channels

Channels

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Channels: the syntax, patterns, and safety habits you need before advancing in Go.

Concurrency is Go’s headline feature—know when to use channels vs mutexes and how to avoid leaks.

You will apply Channels in contexts like: Kubernetes ecosystem tools, cloud APIs, and CLI utilities.

Write Go in main.go with package main and func main(), click Run on server—the dev runner runs go run main.go; use fmt.Println for output (requires Go toolchain; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Channels pass values between goroutines: ch := make(chan int). Send ch <- v, receive v := <-ch. Unbuffered channels synchronize sender and receiver.

Buffered channels

ch := make(chan string, 2)
ch <- "a"
ch <- "b"
fmt.Println(<-ch)

Buffer size allows sends without immediate receive up to capacity. Close channels with close(ch); range over channel until drained.

Directional types

chan<- int send-only, <-chan int receive-only—API design prevents misuse at compile time.

Important interview questions and answers

  1. Q: Buffered vs unbuffered?
    A: Unbuffered blocks until both sides ready (handoff sync); buffered allows queueing up to capacity.
  2. Q: Send on closed channel?
    A: Panics—only sender should close; receivers use comma-ok or range.

Self-check

  1. How do you create a channel of ints?
  2. Who should close a channel?

Closing and ranging

Only the sender should close a channel. Receivers detect close with v, ok := <-ch. for v := range ch drains until close—idiomatic worker shutdown.

Leaking goroutines often means a channel was never closed or a receiver exited early—profile with race detector in real projects.

Pitfall: Sending on a closed channel panics—only the sender should close; receivers use range or comma-ok.

Interview prep

Buffered vs unbuffered?

Unbuffered synchronizes handoff; buffered queues up to capacity before blocking send.

Who closes channel?

Sender closes—receivers detect close via range or comma-ok; send on closed channel panics.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Buffered vs unbuffered?
  • Close channel?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump