以下Go代碼運行正常:爲什麼在if條件中添加括號會導致編譯錯誤?
package main
import "fmt"
func main() {
if j := 9; j > 0 {
fmt.Println(j)
}
}
但添加在條件括號後:
package main
import "fmt"
func main() {
if (j := 9; j > 0) {
fmt.Println(j)
}
}
有編譯錯誤:
.\Hello.go:7: syntax error: unexpected :=, expecting)
.\Hello.go:11: syntax error: unexpected }
爲什麼編譯器抱怨呢?
爲什麼C編譯器會抱怨,如果你省略括號? – Volker