Simple golang program doesn't run

前端 未结 2 502
一生所求
一生所求 2021-01-13 23:50

Here is a simple golang script T1.go:

package main

import \"fmt\"

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

run it with go r

2条回答
  •  梦谈多话
    2021-01-14 00:07

    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.

提交回复
热议问题