dongxian3418 2018-02-02 06:38
浏览 59
已采纳

不能在Go中使用“ go”作为变量名

I think this is little weird, why does this code does not work?

  package main

  import "fmt"

  func main() {
     var i, j int = 1, 2
     k := 3
     c, python, go := true, false, false

     fmt.Println(i, j, k, c, python, go)
  }

Throws error

   # command-line-arguments
   .\compile64.go:8:13: syntax error: unexpected go, expecting expression
   .\compile64.go:10:29: syntax error: unexpected go, expecting expression

But this worked!

   package main

   import "fmt"

   func main() {
      var i, j int = 1, 2
      k := 3
      c, python, goo := true, false, false

      fmt.Println(i, j, k, c, python, goo)
   }

Is "go" a reserved word in Golang?

  • 写回答

2条回答 默认 最新

  • dtgu21994537 2018-02-02 06:41
    关注

    Yes, a keyword:

    break        default      func         interface    select
    case         defer        go           map          struct
    chan         else         goto         package      switch
    const        fallthrough  if           range        type
    continue     for          import       return       var
    

    https://golang.org/ref/spec#Keywords

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?