这个问题在这里已经有了答案:
Shorthand return
(2 个回答)
2年前关闭。
考虑这个 Go 代码(可以很容易地执行 here ):
package main
import "fmt"
func main() {
// initialize an int variable
i := 7
fmt.Printf("%T\n", i) // prints "int"
// runs a simple for loop
for i := 1; i<=5; i++ {
fmt.Printf("Welcome %d times\n",i)
}
fmt.Println(oneLarger(i))
}
// passes arg by VALUE by default
func oneLarger(x int) int {
return x++ // produces compile error
}
对我来说非常有趣的是,当我执行此代码时,它给了我错误“./main.go:23:11: syntax error: unexpected++ at end of statement”。
当我注释掉第 23 行时,注释运行良好;它根本不提示 for 循环中的 i++ 。但是,它不喜欢我函数中的 x++。为什么是这样? Go 中++ 运算符的一般规则是什么?
最佳答案
关于function - Go 中是否允许++?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59207845/