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条)

报告相同问题?