为什么下面的代码会抛出意外的函数错误?我看到错误 ./func_correct.go:4: syntax error: unexpected func, expecting name
package main
func (st *Stack) Pop() int {
v := 0
for ix := len (st) - 1; ix >= 0; ix-- {
if v = st[ix]; v != 0 {
st[ix] = 0
return v
}
}
return 0
}
func main() {
Pop()
}
最佳答案
定义
堆栈
类型在
main
中为其创建一个变量对其调用
Pop
代码:
package main
import "fmt"
type Stack []int
func (st Stack) Pop() int {
v := 0
for ix := len(st) - 1; ix >= 0; ix-- {
if v = st[ix]; v != 0 {
st[ix] = 0
return v
}
}
return 0
}
func main() {
s := Stack{1, 2, 3, 4}
i := s.Pop()
fmt.Println(i)
}
关于go - func 的语法 Golang 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46763057/