douwei6478 2013-10-18 01:52 采纳率: 100%
浏览 489
已采纳

简单的golang程序无法运行

Here is a simple golang script T1.go:

package main

import "fmt"

func main() {
    fmt.Println("Hello world")
}

run it with go run T1.go, I get:

T1.go:1:15: expected ';', found 'import'

If I added ; to line end, the program is okay to run:

package main;

import "fmt";

func main() {
    fmt.Println("Hello world")
}

But isn't the semicolon redundant of line ending in golang?

PS: I am on 64bit window 7, the golang version is devel +f4d1cb8d9a91 or 1.2rc1. The error code runs perfect on http://play.golang.org/

Updates I've used dos2unix to convert the source code to unix line ending, but it doesn't change anything

Notes My go is installed in C:\go directory and the C:\go\bin is added into the %PATH% environment variable; The source code T1.go is put inside the C:\t\go directory, which is different from the go installation directory. Not sure if this configuration contribute to the issue.

  • 写回答

2条回答 默认 最新

  • douzi115522 2013-10-18 08:28
    关注

    Your Hex dump shows that you are using Carriage Return characters (U+000D) instead of LineFeeds (U+000A) in the T1.go file. Using only CR as End-of-line is an old Mac way of doing it.

    The specification states that a new line is a single line feed character. Since this is not found, the parser assumes it is all written on the same line. In such a case, the compiler requires that you actually type out the semi-colons.

    Solution

    Change your CR to LF and it should work.

    If you use Notepad++, you can do this conversion in the menu Edit - EOL Conversion - Unix/OSX Format.

    go fmt does not convert CR to LF, while it does convert CRLF to LF.
    The same goes for dos2unix. In your case, it should work with mac2unix.

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

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据