Golang:Hello world不会打印到屏幕上,程序也不会退出。

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

Golang: Hello world doesn't print to screen and program doesnt exit

问题

当我使用命令"go.exe run main.go"运行以下代码时,程序没有将文本打印到屏幕上,也没有退出。

package main

import "fmt"

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

go.exe版本 = go version go.1.5.1 windows/amd64

set GOARCH=386

set GOBIN=

set GOEXE=.exe

set GOHOSTARCH=386

set GOHOSTOS=windows

set GOOS=windows

set GOPATH=C:\project

set GORACE=

set GOROOT=C:\Go

set GOTOOLDIR=C:\Go\pkg\tool\windows_386

set GO15VENDOREXPERIMENT=

set CC=gcc

set GOGCCFLAGS=-m32 -mthreads -fmessage-length=0

set CXX=g++

set CGO_ENABLED=1

有什么想法是什么出了问题?

谢谢

编辑:

我尝试卸载了windows/amd64版本并安装了windows/386版本,但没有效果。它安装在c:\Go中,并设置了PATH。我使用的是Windows 10。

换行符

package mainLF
LF
import "fmt"LF
LF
func main(){LF
    fmt.Println("Hello world")LF
}LF

以下代码也不会打印到命令提示符。

package main

//import "fmt"

func main(){
    println("Hello world")
}

以下代码会给出错误"fmt imported but not used",所以它一定在做某些事情。

package main

import "fmt"

func main(){

}

GOROOT设置为C:\Go\

PATH: C:\Go\bin

显示我如何运行程序的图片

编辑:新图片

更新的图片

英文:

When I run the following code with the command "go.exe run main.go", the program doesnt print text to the screen or exit.

package main

import "fmt"

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

go.exe version = go version go.1.5.1 windows/amd64

set GOARCH=386

set GOBIN=

set GOEXE=.exe

set GOHOSTARCH=386

set GOHOSTOS=windows

set GOOS=windows

set GOPATH=C:\project

set GORACE=

set GOROOT=C:\Go

set GOTOOLDIR=C:\Go\pkg\tool\windows_386

set GO15VENDOREXPERIMENT=

set CC=gcc

set GOGCCFLAGS=-m32 -mthreads -fmessage-length=0

set CXX=g++

set CGO_ENABLED=1

Any ideas what's wrong?

Thanks

EDIT:

I tried uninstalling the windows/amd64 version and installing windows/386 to no avail. It's installed in c:\Go and the PATH is set. I'm using windows 10.

Line feeds

package mainLF
LF
import "fmt"LF
LF
func main(){LF
    fmt.Println("Hello world")LF
}LF

The following doesn't print to the command prompt either.

package main

//import "fmt"

func main(){
    println("Hello world")
}

The following gives the error "fmt imported but not used" so it must be doing something.

package main

import "fmt"

func main(){

}

GOROOT is set to C:\Go\

PATH: C:\Go\bin

Image showing how I'm running the program

EDIT: New Image

Updated image

答案1

得分: 3

我在Windows 10系统上遇到了同样的问题。运行go run main.go不会打印任何内容,而运行go build main.go,然后运行.\main.exe会正确工作并打印出"hello, world"。

问题是由于Comodo杀毒软件将go.exe进程隔离为其自动隔离功能的一部分。我相信go run命令会编译并尝试将main.go的可执行二进制代码加载到同一进程中,这会触发自动隔离以隔离应用程序的输出。

最初的解决方法是通过Comodo杀毒软件设置禁用自动隔离。禁用此功能后,go run main.go开始正常工作。

永久解决方法是为go.exe可执行文件添加一个"忽略"规则。这样可以启用自动隔离功能,但go run可以正常工作。

英文:

I ran into this same problem on a Windows 10 system. Running go run main.go would not print anything, while go build main.go followed by .\main.exe would work correctly and print out "hello, world".

The problem was due to the Comodo antivirus software quarantining the go.exe process as part of its Auto Containment feature. I believe the go run command compiles and tries to load the executable binary code of main.go into the same process, which triggers the Auto Containment to isolate the output of the application.

The initial fix was to disable Auto Containment via the Comodo Antivirus settings. Upon disabling this feature, go run main.go began to work properly.

The permanent fix was to add an "ignore" rule for the go.exe executable. This allows the Auto Containment feature to be enabled but go run to work properly.

答案2

得分: 1

创建一个名为~/sample的文件夹(其中~表示你的主目录)
你可以通过终端输入以下命令来完成:

mkdir sample

以下是用于显示“Hello World”的程序:

package main
import "fmt"
func main() {
fmt.Println("Hello World")
}

将其保存为main.go文件,保存在我们刚创建的文件夹中。
打开一个新的终端窗口,输入以下命令:

cd sample
go run main.go
英文:

Create a folder named ~/sample (Where ~ means your home directory)
From the terminal you can do this by entering the following commands:

mkdir sample

Below program for hello world display

package main
import "fmt"
func main() {
fmt.Println("Hello World")
}

save it as main.go in the folder we just created.
Open up a new terminal and type in the following:

cd sample
go run main.go

huangapple
  • 本文由 发表于 2015年11月15日 20:30:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/33719671.html
匿名

发表评论

匿名网友

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

确定