Why Switch from PHP to Go? Boost Concurrency with WaitGroup and ErrGroup

This article explains why a backend team moved from PHP to Go for high‑concurrency live‑streaming services, compares serial PHP processing with parallel Go execution, and demonstrates two practical Go concurrency patterns—sync.WaitGroup and errgroup—with complete code examples.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Why Switch from PHP to Go? Boost Concurrency with WaitGroup and ErrGroup

1. Reasons to Choose Go

As a backend developer, the author mainly used PHP and Go. While PHP feels comfortable and fast for simple tasks, it cannot handle the high‑concurrency demands of live‑streaming services. Go, popular among large tech companies, offers simple syntax and strong concurrency support, making it a natural choice.

2. How Go Solves Concurrency Issues

In a typical live‑room request, PHP would fetch version info, live info, user info, equity info, and statistics sequentially, causing the total latency to be the sum of all operations. Go can run these tasks in parallel, so the request time equals the longest single operation.

Two common ways to implement this parallel logic are presented.

Method 1: Using sync.WaitGroup

func main() {
    var (
        VersionDetail, LiveDetail, UserDetail, EquityDetail, StatisticsDetail int
    )
    ctx := context.Background()
    GoNoErr(ctx,
        func() { VersionDetail = 1; time.Sleep(1 * time.Second); fmt.Println("执行第一个任务") },
        func() { LiveDetail = 2; time.Sleep(2 * time.Second); fmt.Println("执行第二个任务") },
        func() { UserDetail = 3; time.Sleep(3 * time.Second); fmt.Println("执行第三个任务") },
        func() { EquityDetail = 4; time.Sleep(4 * time.Second); fmt.Println("执行第四个任务") },
        func() { StatisticsDetail = 5; time.Sleep(5 * time.Second); fmt.Println("执行第五个任务") },
    )
    fmt.Println(VersionDetail, LiveDetail, UserDetail, EquityDetail, StatisticsDetail)
}

func GoNoErr(ctx context.Context, functions ...func()) {
    var wg sync.WaitGroup
    for _, f := range functions {
        wg.Add(1)
        go func(function func()) {
            function()
            wg.Done()
        }(f)
    }
    wg.Wait()
}

Method 2: Using errgroup library

func main() {
    var (
        VersionDetail, LiveDetail, UserDetail, EquityDetail, StatisticsDetail int
        err error
    )
    ctx := context.Background()
    err = GoErr(ctx,
        func() error { VersionDetail = 1; time.Sleep(1 * time.Second); fmt.Println("执行第一个任务"); return nil },
        func() error { LiveDetail = 2; time.Sleep(2 * time.Second); fmt.Println("执行第二个任务"); return nil },
        func() error { UserDetail = 3; time.Sleep(3 * time.Second); fmt.Println("执行第三个任务"); return nil },
        func() error { EquityDetail = 4; time.Sleep(4 * time.Second); fmt.Println("执行第四个任务"); return nil },
        func() error { StatisticsDetail = 5; time.Sleep(5 * time.Second); fmt.Println("执行第五个任务"); return nil },
    )
    if err != nil { fmt.Println(err); return }
    fmt.Println(VersionDetail, LiveDetail, UserDetail, EquityDetail, StatisticsDetail)
}

func GoErr(ctx context.Context, functions ...func() error) error {
    var eg errgroup.Group
    for i := range functions {
        f := functions[i]
        eg.Go(func() error { return f() })
    }
    return eg.Wait()
}

Both approaches can be used directly; they are fundamental Go concurrency techniques that split a parent task into multiple child tasks, greatly reducing overall latency.

Key tip: when creating closures inside a loop, capture the loop variable by assigning it to a new variable (e.g., fs := f) to avoid all closures referencing the same final value.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendGolangPHPerrgroupWaitGroup
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.