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()
}

图片转代码服务由CSDN问答提供 功能建议

我的输出显示

。\ main1.go:8:无效的标识符 字符U + 200b

。\ main1.go:8:未定义:fmt.Print中的fmt

我什至在操场上也尝试过

有人用相同的代码发布了答案“ 0 1 2 3”。

我为什么要复制相同的代码,但是

 包main 
 
import“ fmt” 
 
func main(){
x:= [] string {“ i”,“ j”,  “ k”,“ l”} 
对于v:=范围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

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douzong5835 2016-05-27 14:50

    U+200b is apparently the zero-width space. It probably appeared after a weird copy-paste.

    Try to rewrite the code from scratch.

    评论
    解决 无用
    打赏 举报
  • download20151010 2016-05-27 14:53

    Wherever you copied it from copied some additional characters, specifically a unicode zero-dith space, and put them in between the opening brace in the for ... range statement, and the fmt.Print(...). Looking at the paste output in a hex editor reveals this is the case. If you select and delete this character which is prior to the fmt.Print, go fmt will run again and the program will compile. Deleting all the white space before that statement works, too.

    评论
    解决 无用
    打赏 举报
  • doushan6692 2016-05-28 14:18

    The line 8 of your code has Zero Width Space character, which is a non-printable character. This could be in the source you copied from.

    Re-writing the line after removing spaces as below works fine.

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

    Output:

    0 1 2 3

    When you run go fmt on your code, it says filename.go:8:5: illegal character U+200B, which specifies the position of ZWSP.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题