将变量括在Go中的括号中

yan*_*nis 0 go

我正在努力了解它的(*s)作用,我会很感激解释.

// pop pops the stack. It will panic if s is empty.
func (s *nodeStack) pop() *Node {
    i := len(*s)
    n := (*s)[i-1]
    *s = (*s)[:i-1]
    return n
}
Run Code Online (Sandbox Code Playgroud)

jre*_*ior 5

s是一个指针. *s是它指向的东西.括号是要清楚 - 既包括编译器中的解析器,也包括人类读者 - 为数组样式索引指定了哪一个.也就是说,它是消除歧义:

(*s)[i-1]
Run Code Online (Sandbox Code Playgroud)

*(s[i-1])
Run Code Online (Sandbox Code Playgroud)