I'm starting using Golang as my main programming language to do exercises in LeetCode, and I get a strange result for Golang's := operator for multiple variables.
Let me say the question 104 to get the maxDepth of a binary tree.
My first solution is to use layer traversing, and below is my code.
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func maxDepth(root *TreeNode) int {
    if root == nil {
        return 0
    }
    var s []*TreeNode
    var cur *TreeNode
    depth := 0
    s = append(s, root)
    for len(s) != 0 {
        size := len(s)
        depth++
        for i := size; i > 0; i-- {
            s, cur = s[1:], s[0]
            if cur.Left != nil {
                s = append(s, cur.Left)
            }
            if cur.Right != nil {
                s = append(s, cur.Right)
            }
        }
    }
    return depth
}
The upper code snippet can pass the test. But the below code snippet can't, and I don't know why, I just can debug the for loop can't finish, and it becomes an infinite loop.
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func maxDepth(root *TreeNode) int {
    if root == nil {
        return 0
    }
    var s []*TreeNode
    depth := 0
    s = append(s, root)
    for len(s) != 0 {
        size := len(s)
        depth++
        for i := size; i > 0; i-- {
            s, cur := s[1:], s[0]
            if cur.Left != nil {
                s = append(s, cur.Left)
            }
            if cur.Right != nil {
                s = append(s, cur.Right)
            }
        }
    }
    return depth
}
I hope anyone can explain to me how Golang handles the := operators for multiple variables and at least one of which has already been declared in some other place. Also, you can put some official links which can prove your explanation.