英文:
Hello World syntax error
问题
当我尝试使用6g编译器编译来自go语言网站首页的示例时,我遇到了以下错误:
hello.go:5: 语法错误,附近有“<string>”
我在Google上进行了搜索,发现有一些人遇到了这个问题,但我没有找到解决办法。答案似乎总是:“对我来说可以工作,你一定做错了什么”。
我找到了一个问题的描述,时间可以追溯到5个月前,所以我怀疑这不是我使用的特定版本的go的问题。此外,我尝试拉取了一个更新的版本,问题仍然存在。
相关的源代码:
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
}
顺便说一下,我将源代码保存为UTF-8编码,并使用LF作为换行符。这不应该是文本编码的问题。我还尝试了不包含“奇特”字符的不同字符串,问题仍然存在。
英文:
When I try to compile the example from the <a href="golang.org">front page of the go language website</a> with the 6g compiler, I get this error:
hello.go:5: syntax error near "<string>"
I <a href="https://groups.google.com/group/golang-nuts/browse_thread/thread/300a24d431799ea5">search on Google reveals</a> that a few people have experienced this, but I have found no solution. The answer always seems to be: "It's works for me, you must do something wrong".
I've found a <a href="https://groups.google.com/group/golang-nuts/browse_thread/thread/9d515a056112c752/ef8d29b46f9bc384?lnk=gst&q=hello+world#ef8d29b46f9bc384">description of the problem</a> that dates back 5 months, so I suspect it's not a problem with the particular build of go that I'm using. Besides, I've tried pulling a newer version, and the problem persists.
The source code in question:
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
}
Btw, I'm saving the source code as UTF-8 with LFs for newlines. It shouldn't be a text encoding issue. I've also tried with different strings not containing "exotic" characters
答案1
得分: 1
你在代码中使用的特殊字符可能会与编译器发生冲突。尝试使用记事本以多种方式保存此代码(ANSI、UTF-8),看看编译器是否能够接受其中任何一种方式。
英文:
Your special characters in there might cause conflicts with the compiler. Try to save this code in multiple ways using notepad (ANSI, UTF-8), and see whether the compiler will take any of them.
答案2
得分: 1
当出现编码问题时,通常会出现这样的问题。
如果您使用的是Windows操作系统,可以使用像Notepad++这样的编辑器在多种编码格式之间进行转换,因此我建议将您的源代码转换为UTF-8无BOM格式,然后重新编译。
如果您使用的是Linux操作系统,有一个可用的指南可以向您展示如何确定和更改文档的编码。
英文:
Problems like this are typical when there's an encoding issue.
If you're on Windows, an editor like Notepad++ can convert between many encoding formats, so I'd suggest converting your source to UTF-8 without BOM and then recompile.
If you're on Linux, there's a guide available showing you how to determine and change a document's encoding.
答案3
得分: 1
尝试执行"which 6g"命令。
你可能正在使用一个旧版本的构建。
至少这是我的情况。我的路径中有一个旧的2009版本。
修复环境后,它就可以正常工作了。
英文:
Try "which 6g".
You might be picking up an old build.
At least that was my case. I had an old 2009 build in my path.
After fixing the environment it worked.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论