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

简单的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.

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

这是一个简单的golang脚本T1.go:

  包main 
 
导入“ fmt” 
 
func main(){
 fmt.Println(“ H​​ello world”)
} 
   
 
 

使用 go run T1.go ,我得到:

  T1.go:1:15:预期为';',找到了'import'
    
 
 

如果我在行尾添加了; ,则该程序可以运行:

  package main; 
 
import“ fmt”; 
 
func main(){
 fmt.Println(“ H​​ello world”)
} 
   
 
 

但是不是以golang结尾的行的分号多余吗?

PS:我在64位窗口7上,golang版本是 devel + f4d1cb8d9a91 或< 强> 1.2rc1 即可。 error 代码在 http://play.golang.org/ <上完美运行 / p>

更新我已经使用 dos2unix 将源代码转换为unix行结尾,但是它没有任何改变

注释我的围棋安装在 C:\ go 目录中,并添加了 C:\ go \ bin 放入%PATH%环境变量中; 源代码 T1.go 放置在 C:\ t \ go 目录中,该目录与go安装目录不同。 不确定此配置是否会导致此问题。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dozpv84422 2013-10-18 06:16

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

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题