Golang中的无限for循环[重复]

This question already has an answer here:

I'm new to Golang, but I would expect not to have issues with something as basic as this.

package main

import "fmt"

func main() {
    s := make([]int, 0)
    s = append(s, 1)
    for len(s) != 0 {
        j := len(s) - 1
        top, s := s[j], s[:j]
        fmt.Printf("top = %+v
", top)
        fmt.Printf("s = %+v
", s)
        fmt.Printf("len(s) = %+v
", len(s))
    }
}

This command doesn't exit, it just prints

len(s) = 0
top = 1
s = []
len(s) = 0
top = 1
s = []
len(s) = ^C

I find this stunning; what am I doing wrong? Syntactically, based on https://tour.golang.org/flowcontrol/3, everything seems OK.

</div>

When you use :=, you declare new variables. An s is created inside the loop unrelated to the s outside it. Assign instead:

for len(s) != 0 {
    j := len(s) - 1
    var top int
    top, s = s[j], s[:j]
    fmt.Printf("top = %+v
", top)
    fmt.Printf("s = %+v
", s)
    fmt.Printf("len(s) = %+v
", len(s))
}