CodeBlanks Logo Icon CodeBlanks

Summary - Channels

Combine channel concepts to write a worker.

Summary - Channels

In this section you learned channels:

  • Create a channel with make(chan T) for typed communication
  • Send safely with ch <- v and understand blocking
  • Receive with <-ch and check status
  • Buffer with buffered channels for async starts

Now you will combine these to write a worker: create a channel, start a goroutine, receive and print.

Practice Worker channel

Inside main, add a make(chan string) call and print worker done with fmt.Println.

Starter code

main.go
package main

import "fmt"

func main() {
    // buraya yaz
}

Sample solution

main.go
package main

import "fmt"

func main() {
    ch := make(chan string)
    go func() { ch <- "worker tamam" }()
    fmt.Println(<-ch)
}

Expected output

worker tamam

Practice Worker output

Inside main, print channel worker with fmt.Println.

Starter code

main.go
package main

import "fmt"

func main() {
    // buraya yaz
}

Sample solution

main.go
package main

import "fmt"

func main() {
    fmt.Println("channel worker")
}

Expected output

channel worker

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Channel usage

Question
ch := make(chan string)
go func() {
    ch <- ____
}()
msg := <-ch
Answer
ch := make(chan string)
go func() {
    ch <- "ok"
}()
msg := <-ch

Explanation: <-ch is receive, not send.

2 Match section concepts

Term Meaning
make(chan int) Creation
ch <- v Send
<-ch Receive
make(chan int, n) Buffered

All lessons in Intermediate