问题描述

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

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结尾的分号不是多余的吗?

devel +f4d1cb8d9a91
dos2unix
C:\goC:\go\bin%PATH%T1.goC:\t\go
T1.go

规范指出,新行是单个换行字符.由于找不到此内容,因此解析器假定所有内容都写在同一行上.在这种情况下,编译器要求您实际键入分号.

解决方案

将您的CR更改为LF,它应该可以工作.

如果使用Notepad ++,则可以在编辑""EOL转换""Unix/OSX格式"菜单中进行此转换.

go fmtdos2unixmac2unix

Here is a simple golang script T1.go:

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

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

devel +f4d1cb8d9a91
dos2unix
C:\goC:\go\bin%PATH%T1.goC:\t\go
T1.go

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 fmtdos2unixmac2unix

这篇关于简单的golang程序无法运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!