Goroutines and channels: work at the same time

Learn Go · Beginner · 12 min · After Generics in Go: one function, many types

How does Go run several things at once without shared-memory bugs?

  1. 1Guess

    package main
    
    import "fmt"
    
    func main() {
    	ch := make(chan int)
    	go func() {
    		for i := 1; i <= 3; i++ {
    			ch <- i
    		}
    		close(ch)
    	}()
    	for v := range ch {
    		fmt.Println(v)
    	}
    }
    
    What does this program print?
  2. 2Read

    Goroutines

    Put go before a function call and it runs as a goroutine, at the same time as the rest of your code. When main returns, the program ends, even if goroutines are still running. To wait for them, use a sync.WaitGroup: call wg.Add(1) before starting, defer wg.Done() inside the goroutine and wg.Wait() where you need all of them finished.

  3. 3Code

    Three workers and a WaitGroup

    1 / 2
    package main
    
    import (
    	"fmt"
    	"sync"
    )
    
    func main() {
    	results := make(chan int, 3)
    	var wg sync.WaitGroup
    	for i := 1; i <= 3; i++ {
    		wg.Add(1)
    		go func() {
    			defer wg.Done()
    			results <- i * i
    		}()
    	}
    	wg.Wait()
    	close(results)
    
    	sum := 0
    	for r := range results {
    		sum += r
    	}
    	fmt.Println("sum:", sum)
    }
    

    Read the program. What will it print?

    Run it in the playground

  4. 4Read

    Channels

    A channel moves values between goroutines: ch <- v sends and v := <-ch receives. On an unbuffered channel, a send waits until another goroutine receives. A buffered channel, make(chan int, 3), lets sends continue until it is full. close(ch) tells receivers no more values come, and range stops then. select waits on several channels; default runs when none is ready.

  5. 5Code

    A pipeline and a select

    1 / 2
    package main
    
    import "fmt"
    
    func generate(n int) <-chan int {
    	out := make(chan int)
    	go func() {
    		for i := 1; i <= n; i++ {
    			out <- i
    		}
    		close(out)
    	}()
    	return out
    }
    
    func square(in <-chan int) <-chan int {
    	out := make(chan int)
    	go func() {
    		for v := range in {
    			out <- v * v
    		}
    		close(out)
    	}()
    	return out
    }
    
    func main() {
    	for v := range square(generate(4)) {
    		fmt.Println(v)
    	}
    
    	empty := make(chan int)
    	select {
    	case v := <-empty:
    		fmt.Println("got", v)
    	default:
    		fmt.Println("nothing ready")
    	}
    }
    

    Read the program. What will it print?

    Run it in the playground

  6. 6Fix

    package main
    
    import "fmt"
    
    func main() {
    	ch := make(chan int)
    	ch <- 1
    	fmt.Println(<-ch)
    }
    
    The program stops with a deadlock. What is wrong?
  7. 7Say it back

  8. 8Mission · 12 min

    Build a worker pool: three workers read the numbers 1 to 9 from a jobs channel and send their squares to a results channel. Add up the results and print the total, which must be 285.

    go run .

Common questions

What is a goroutine?
A goroutine is a function running at the same time as other code in the same program. You start one by putting go before a call. Goroutines are cheap, so a program can run thousands of them. They share memory, so Go advice is to pass values over channels instead of sharing variables, which avoids many timing bugs.
Why does my program end before the goroutine prints?
When main returns, the whole program exits, and it does not wait for goroutines that are still running. You must make main wait. The usual tools are a sync.WaitGroup, which counts running goroutines, or a channel that main receives from. A sleep is not a fix, because it only guesses how long the work takes.
When should I close a channel?
Close a channel when the sender has no more values, so receivers using range or the two-value receive know to stop. Only the sender should close it, and never more than once, or the program panics. You do not have to close a channel for cleanup; the garbage collector frees it when nothing uses it.

Sources

Measured with go1.27.4 darwin/arm64 on 2026-12-03

Published Dec 3, 2026 · Updated Dec 3, 2026

Never miss new offers and news

New tools, credits, and AI news are added regularly. Pick how you want to hear about them.

Telegram channel

Join @aifree_dev and get new free AI offers and news right in your Telegram feed.

Browser notifications

Turn on push notifications and get an alert on this device whenever something new is posted. No account needed.

Email newsletter

A weekly digest of new, changed and expiring free AI offers. Double opt-in, one-click unsubscribe. See the latest issue.

Unsubscribe anytime. Privacy

Frequently asked questions

What is aifree.dev?

aifree.dev helps everyone access AI through a curated directory of free AI tools, API credits, free tiers, and startup programs for coding, image, voice, video, and students - verified regularly.

How are offers presented?

Each offer includes its access details, free models when available, and expiration information when provided. Limited-time deals stay visible so they are easy to compare.

Do education and startup programs need verification?

Yes. Providers usually ask students to verify their status with a school email, student ID, or a program like the GitHub Student Pack, and ask startups to verify company details such as age, funding, or website. Check each offer’s claim page for the exact requirements.

What kinds of free AI offers are listed?

Free tiers and free trials from AI providers, free API credits, startup and student programs, and free tools for coding, image generation, voice, and video.

How can I suggest a tool or report a broken link?

Use the submit form: paste the offer page URL and a short note. A person checks every submission before anything is listed. Broken links and changed terms go through the same form.

Know a good one?

Tell us about an AI tool worth knowing. Found a broken link? We want to hear that too.

Submit an offer