One idea
A goroutine runs a function at the same time as the rest of your program. A channel lets that goroutine send a typed value back safely.
Use this for work that can happen in the background, like logging, sending a notification, checking a slow dependency, or preparing a report.
Where it fits
Your service does not need complex concurrency to look serious. One clear use is enough for this track.
For a college project, you can record a visit count, run a fake email receipt, or calculate a summary in the background after an HTTP request.
Use the pattern
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done
Small worker example
This example calculates a total in a goroutine and sends it back on a channel. It uses only the standard library.
A channel made with make(chan int) is unbuffered. The send waits until another goroutine receives the value.
package main
import "fmt"
type Order struct {
ID string
Amount int
}
func totalAsync(orders []Order) <-chan int {
result := make(chan int)
go func() {
total := 0
for _, order := range orders {
total += order.Amount
}
result <- total
}()
return result
}
func main() {
orders := []Order{
{ID: "upi-101", Amount: 199},
{ID: "upi-102", Amount: 249},
}
totalCh := totalAsync(orders)
fmt.Println("doing other work")
total := <-totalCh
fmt.Println("total rupees:", total)
}Use direction
Return a receive-only channel when callers should only read from it. That is why the example returns <-chan int.
This makes your intent clear and prevents accidental sends from the wrong place.
Avoid goroutine leaks
Do not start a goroutine that sends to a channel if nobody will receive from it. It can wait forever.
Also do not add goroutines just to look advanced. If normal code is clearer, use normal code.
Buffered channel option
A channel made with make(chan string, 1) has space for one value. The sender can place one value without waiting for the receiver immediately.
This is useful for small one-shot signals, not for hiding slow code.
func sendReceiptAsync(orderID string) <-chan string {
done := make(chan string, 1)
go func() {
// Replace this later with real email, SMS, or webhook code.
done <- "receipt queued for " + orderID
}()
return done
}Do this now
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done