简单的 Golang 程序无法运行

huangapple go评论69阅读模式
英文:

Simple golang program doesn't run

问题

以下是T1.go的简单golang脚本:

package main

import "fmt"

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

使用go run T1.go运行它,我得到以下错误:

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

如果我在行末添加;,程序就可以正常运行:

package main;

import "fmt";

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

但是在golang中,分号不是行尾的多余符号吗?

PS:我使用的是64位的Windows 7,golang版本是devel +f4d1cb8d9a911.2rc1。这段错误代码在http://play.golang.org/上运行得很好。

更新:我使用了dos2unix将源代码转换为Unix行尾符,但没有改变任何东西。

注意:我的go安装在C:\go目录下,C:\go\bin已添加到%PATH%环境变量中;源代码T1.go放在C:\t\go目录下,这与go安装目录不同。不确定这个配置是否导致了问题。

英文:

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.

答案1

得分: 8

你的十六进制转储显示,在T1.go文件中,你使用的是回车字符(U+000D),而不是换行符(U+000A)。只使用CR作为行尾是旧版Mac的做法。

规范指出,换行是一个单独的换行符字符。由于找不到这个字符,解析器会认为所有内容都写在同一行上。在这种情况下,编译器要求你实际上输入分号。

解决方案

将你的CR更改为LF,应该就可以正常工作。

如果你使用Notepad++,你可以在菜单"Edit - EOL Conversion - Unix/OSX Format"中进行此转换。

go fmt不会将CR转换为LF,但会将CRLF转换为LF。
对于dos2unix也是一样。在你的情况下,应该可以使用mac2unix来解决问题。

英文:

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.

答案2

得分: -2

似乎是1.2rc1版本中的一个错误。尝试使用1.2rc2版本,看看问题是否仍然存在。

英文:

Sounds like a bug in the 1.2rc1 version. Try the 1.2rc2 and see if the problem is still there.

huangapple
  • 本文由 发表于 2013年10月18日 09:52:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/19440301.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定