10 Common Go Pitfalls Every PHP Developer Should Avoid

This article shares ten frequent mistakes PHP developers encounter when switching to Go, covering function syntax, map initialization, JSON marshaling, loop variable handling, array vs slice semantics, and variable declaration nuances, each with problem description and corrected code examples.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
10 Common Go Pitfalls Every PHP Developer Should Avoid

As a PHP developer with five years of experience, I have gradually shifted new projects from PHP to Go. During this transition I encountered several common pitfalls, which I summarize here.

1. Function Definition

Problem: The opening brace is placed on a new line, which is invalid in Go.

func main()
{
    fmt.Println("php是世界上最好的语言")
}

Solution: The brace must be on the same line as the function name.

func main() {
    fmt.Println("php是世界上最好的语言")
}

2. Map Definition and Initialization

Problem: Declaring a map without allocating memory causes a panic.

func main() {
    var m map[string]string
    m["php"] = "世界上最好的语言"
    m["go"]  = "世界上最好的语言"
    fmt.Println(m)
}

Solution: Use make to allocate the map before use.

func main() {
    m := make(map[string]string, 2)
    m["php"] = "世界上最好的语言"
    m["go"]  = "世界上最好的语言"
    fmt.Println(m)
}

3. JSON Marshalling

Problem: Struct fields are lowercase, making them unexported and resulting in empty JSON output.

type Student struct {
    id    int
    name  string
    score int
}

func main() {
    s := Student{1, "小明", 99}
    jsonS, _ := json.Marshal(s)
    fmt.Println(string(jsonS))
}

Solution: Export fields by capitalizing their names.

type Student struct {
    Id    int
    Name  string
    Score int
}

func main() {
    s := Student{1, "小明", 99}
    jsonS, _ := json.Marshal(s)
    fmt.Println(string(jsonS))
}

4. Modifying Elements Inside a Loop

Problem: Updating the loop variable does not change the original slice.

func main() {
    data := []int{1, 2, 3}
    for _, value := range data {
        value += 1
    }
    fmt.Println(data)
}

Solution: Use the index to modify the slice directly.

func main() {
    data := []int{1, 2, 3}
    for i := range data {
        data[i] += 1
    }
    fmt.Println(data)
}

5. Array vs. Slice Differences

Problem: Passing an array to a function results in value‑copy, so modifications are not reflected.

func change(data [2]int) {
    data[0] = 4
}

func main() {
    data := [2]int{1, 2}
    change(data)
    fmt.Println(data)
}

Solution: Use a slice, which shares the underlying array.

func change(data []int) {
    data[0] = 4
}

func main() {
    data := []int{1, 2}
    change(data)
    fmt.Println(data)
}

6. := Declaration and Variable Scope

Problem: Re‑declaring a variable with := inside an if block creates a new variable, leaving the outer one unchanged.

func main() {
    flag := 1
    if true {
        flag := 2
        flag++
    }
    fmt.Println(flag)
}

Solution: Use assignment ( =) to modify the existing variable.

func main() {
    flag := 1
    if true {
        flag = 2
        flag++
    }
    fmt.Println(flag)
}

Summary: These are some of the most common beginner pitfalls when moving from PHP to Go, covering syntax, data structures, JSON handling, loop semantics, and variable scope. Understanding and avoiding them can greatly smooth the learning curve.

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.

BackendprogrammingPHPTutorialPitfalls
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.