dpi9530 2016-05-27 14:40
浏览 165
已采纳

这个golang代码的输出是什么?

My output says

.\main1.go:8: invalid identifier character U+200b

.\main1.go:8: undefined: ​fmt in ​fmt.Print

I even tried it on go playground same response.

Someone posted an answer "0 1 2 3" with the same code.

How come I copied the same code but with the above result.

package main

import "fmt"

func main() {
x := []string{"i","j","k","l"}
for v := range x {
  ​fmt.Print(v, " ")
  }
fmt.Println()
}
  • 写回答

4条回答 默认 最新

  • douyi3676 2016-05-27 20:37
    关注

    there are 2 things to note:
    first: if you view your source file with simple Hex Viewer, you will see some extra hex bytes before fmt.Print(v, " "):
    20 20 E2 80 8B
    lets delete it and now we have:

    package main
    
    import "fmt"
    
    func main() {
        x := []string{"i", "j", "k", "l"}
        for v := range x {
            fmt.Print(v, " ")
        }
        fmt.Println()
    }
    

    now the output is:
    0 1 2 3

    second:
    but normally we use variable names like v for value and var named i for index,
    so it seems the code needs attention:

    package main
    
    import "fmt"
    
    func main() {
        x := []string{"i", "j", "k", "l"}
        for _, v := range x {
            fmt.Print(v, " ")
        }
        fmt.Println()
    }
    

    now output is:
    i j k l

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

报告相同问题?

悬赏问题

  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding