PY
GO

Python to Go

10 lessons

Progress0%
1Variables & Types2Functions3Lists → Slices, Dicts → Maps4Classes → Structs5asyncio → Goroutines6Modules → Packages7Interfaces8Error Handling Patterns9Testing10Standard Library
All Mirror Courses
PY
GO
asyncio → Goroutines
MirrorLesson 5 of 10
Lesson 5

asyncio → Goroutines

Concurrent programming

Introduction

In this lesson, you'll learn about asyncio → goroutines in Go. Coming from Python, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
PY
From Python:

In Python, you're familiar with concurrent programming.

GO
In Go:

Go has its own approach to concurrent programming, which we'll explore step by step.

The Go Way

Let's see how Go handles this concept. Here's a typical example:

GO
Go Example
package main

import (
    "fmt"
    "io"
    "net/http"
    "sync"
)

func fetch(url string) (string, error) {
    resp, err := http.Get(url)
    if err != nil { return "", err }
    defer resp.Body.Close()
    body, err := io.ReadAll(resp.Body)
    return string(body), err
}

func main() {
    urls := []string{"http://api/a", "http://api/b"}
    results := make([]string, len(urls))
    var wg sync.WaitGroup

    for i, url := range urls {
        wg.Add(1)
        go func(i int, url string) {
            defer wg.Done()
            results[i], _ = fetch(url)
        }(i, url)
    }
    wg.Wait()
    fmt.Println(results)
}

Comparing to Python

Here's how you might have written similar code in Python:

PY
Python (What you know)
import asyncio
import aiohttp

async def fetch(session, url: str) -> str:
    async with session.get(url) as r:
        return await r.text()

async def main():
    async with aiohttp.ClientSession() as s:
        # Parallel requests
        results = await asyncio.gather(
            fetch(s, "http://api/a"),
            fetch(s, "http://api/b"),
        )
    print(results)

asyncio.run(main())
Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

GO
In Go:

Go goroutines use real threads; Python asyncio is single-threaded

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

GO
In Go:

asyncio.gather → sync.WaitGroup or channels

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

GO
In Go:

Go http.Get is synchronous but cheap in goroutines; Python needs async client

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

GO
In Go:

Go can use all CPU cores; Python GIL limits CPU parallelism

Step-by-Step Breakdown

1. go Keyword

The 'go' keyword launches a goroutine — a lightweight thread. Unlike Python's async, no await needed; the goroutine runs concurrently.

PY
Python
result = await fetch(session, url)
GO
Go
go func() { result, _ = fetch(url) }()

2. WaitGroup vs gather

sync.WaitGroup waits for a set of goroutines to complete — the Go equivalent of asyncio.gather.

PY
Python
results = await asyncio.gather(*tasks)
GO
Go
var wg sync.WaitGroup
for ... { wg.Add(1); go func() { defer wg.Done(); work() }() }
wg.Wait()

3. Channels for Results

Go channels safely pass data between goroutines — replacing asyncio's return values from gather.

GO
Go
ch := make(chan string, len(urls))
for _, url := range urls {
    go func(u string) { r, _ := fetch(u); ch <- r }(url)
}
for range urls { fmt.Println(<-ch) }

Common Mistakes

When coming from Python, developers often make these mistakes:

  • Go goroutines use real threads; Python asyncio is single-threaded
  • asyncio.gather → sync.WaitGroup or channels
  • Go http.Get is synchronous but cheap in goroutines; Python needs async client
Common Pitfall
Don't assume Go works exactly like Python. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • go keyword launches goroutines (truly parallel)
  • asyncio.gather → WaitGroup or channels
  • Goroutines use OS threads; asyncio uses event loop
  • Go uses all CPU cores; Python GIL prevents this
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Python code in Go to practice these concepts.
PreviousNext