Common Go Interview Pitfalls and How to Fix Them
This article walks through six common Go interview mistakes—incorrect function brace placement, map initialization without make, unexported struct fields for JSON, ineffective slice iteration, array versus slice parameter passing, and variable shadowing with :=—explaining each issue and providing the correct code examples.
1. Function definition line break
In Go the opening brace must be on the same line as the function declaration; placing it on a new line causes a compilation error.
func main()
{
fmt.Println("php是世界上最好的语言")
}Correct form:
func main() {
fmt.Println("php是世界上最好的语言")
}2. Map definition and initialization
Declaring a map variable without allocating memory leads to a runtime panic when assigning values. Use make to create the map before use.
func main() {
var m map[string]string
m["php"] = "世界上最好的语言"
m["go"] = "世界上最好的语言"
fmt.Println(m)
}Correct form:
func main() {
m := make(map[string]string, 2)
m["php"] = "世界上最好的语言"
m["go"] = "世界上最好的语言"
fmt.Println(m)
}3. JSON struct field export
Only exported (capitalized) struct fields are marshaled to JSON. Using lowercase field names results in an empty JSON object.
type Student struct {
id int
name string
score int
}
func main() {
s := Student{1, "小明", 99}
jsonS, _ := json.Marshal(s)
fmt.Println(string(jsonS))
}Correct form with exported fields:
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 slice elements inside a range loop
Iterating with for _, value := range data gives a copy of each element; modifying value does not affect the original slice.
func main() {
data := []int{1, 2, 3}
for _, value := range data {
value += 1
}
fmt.Println(data) // prints [1 2 3]
}Correct approach modifies the slice by index:
func main() {
data := []int{1, 2, 3}
for i := range data {
data[i] += 1
}
fmt.Println(data) // prints [2 3 4]
}5. Array vs. slice parameter passing
Arrays are passed by value, so changes inside a function do not affect the caller. Slices share the underlying array, allowing modifications to be visible outside.
func change(data [2]int) {
data[0] = 4
}
func main() {
data := [2]int{1, 2}
change(data)
fmt.Println(data) // prints [1 2]
}Using a slice fixes the issue:
func change(data []int) {
data[0] = 4
}
func main() {
data := []int{1, 2}
change(data)
fmt.Println(data) // prints [4 2]
}6. Variable shadowing with :=
Using := inside a block creates a new variable that shadows the outer one, leaving the outer variable unchanged.
func main() {
flag := 1
if true {
flag := 2
flag++
}
fmt.Println(flag) // prints 1
}Replace the inner := with an assignment = to modify the original variable:
func main() {
flag := 1
if true {
flag = 2
flag++
}
fmt.Println(flag) // prints 3
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Go Development Architecture Practice
Daily sharing of Golang-related technical articles, practical resources, language news, tutorials, real-world projects, and more. Looking forward to growing together. Let's go!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
