在Windows 10上出现Go程序挂起的问题。

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

Go programs hanging on Windows 10

问题

我一直在Windows上遇到所有Go程序挂起的问题,即使是一个简单的Hello world程序也是如此。我已经多次重新安装了Golang,但没有任何改变。我也无法使用Ctrl+X或Ctrl+D来关闭程序。它仍然会生成一个进程,并在进程监视器中显示出来。但是我无法从那里或从命令行中终止它。

Reddit上有一个类似的问题帖子https://www.reddit.com/r/golang/comments/2lvnqk/not_even_hello_world_works/。但是那个帖子仍然没有解决方案。

这是我现在尝试运行的程序:

package main

import "fmt"

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

使用go run -x运行它会得到以下输出:

WORK=C:\Users\User\AppData\Local\Temp\go-build882050150
mkdir -p $WORK\command-line-arguments\_obj\
mkdir -p $WORK\command-line-arguments\_obj\exe\
cd E:\go\src\github.com\test\hello
"C:\\Go\\pkg\\tool\\windows_amd64\\compile.exe" -o "C:\\Users\\User\\AppData\\Local\\Temp\\go-build882050150\\command-line-arguments.a" -trimpath "C:\\Users\\User\\AppData\\Local\\Temp\\go-build882050150" -p main -complete -buildid 0180ed6e175ea3e4bc497fc21fe0319a733a9c8e -D _/E_/go/src/github.com/test/hello -I "C:\\Users\\User\\AppData\\Local\\Temp\\go-build882050150" -pack "E:\\go\\src\\github.com\\test\\hello\\main.go"
cd .
"C:\\Go\\pkg\\tool\\windows_amd64\\link.exe" -o "C:\\Users\\User\\AppData\\Local\\Temp\\go-build882050150\\command-line-arguments\\_obj\\exe\\main.exe" -L "C:\\Users\\User\\AppData\\Local\\Temp\\go-build882050150" -w -extld=gcc -buildmode=exe -buildid=0180ed6e175ea3e4bc497fc21fe0319a733a9c8e "C:\\Users\\User\\AppData\\Local\\Temp\\go-build882050150\\command-line-arguments.a"
$WORK\command-line-arguments\_obj\exe\main.exe

之后它就会停在那里什么都不做。无论运行什么程序,它都会永远挂起。

不知道接下来该怎么办。我不常在Windows上开发,但我想尝试在Ubuntu上进行交叉编译,并在交叉编译的二进制文件中遇到了这个问题,所以我想尝试直接在Windows上编译。但是似乎我的Windows不喜欢Golang编译的二进制文件。

Go版本是1.5.1 windows/amd64。

这是go env的输出:

set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=E:\go
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GO15VENDOREXPERIMENT=
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1
英文:

I've been having issues with all Go programs hanging on Windows, even a simple Hello world program. I've reinstalled Golang multiple times with nothing changing. I can't use Ctrl+X or Ctrl+D to close the program either. It'll still spawn the process which will show up on Process Monitor. But I can't kill it from there or from the command line.

There was a similar issue to this posted on reddit https://www.reddit.com/r/golang/comments/2lvnqk/not_even_hello_world_works/. But that still has no resolution.

The is the program I'm trying to get to run right now:

package main

import "fmt"

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

Running it with go run -x gives this output:

WORK=C:\Users\User\AppData\Local\Temp\go-build882050150
mkdir -p $WORK\command-line-arguments\_obj\
mkdir -p $WORK\command-line-arguments\_obj\exe\
cd E:\go\src\github.com\test\hello
"C:\\Go\\pkg\\tool\\windows_amd64\\compile.exe" -o "C:\\Users\\User\\AppData\\Local\\Temp\\go-build882050150\\command-line-arguments.a" -trimpath "C:\\Users\\User\\AppData\\Local\\Temp\\go-build882050150" -p main -complete -buildid 0180ed6e175ea3e4bc497fc21fe0319a733a9c8e -D _/E_/go/src/github.com/test/hello -I "C:\\Users\\User\\AppData\\Local\\Temp\\go-build882050150" -pack "E:\\go\\src\\github.com\\test\\hello\\main.go"
cd .
"C:\\Go\\pkg\\tool\\windows_amd64\\link.exe" -o "C:\\Users\\User\\AppData\\Local\\Temp\\go-build882050150\\command-line-arguments\\_obj\\exe\\main.exe" -L "C:\\Users\\User\\AppData\\Local\\Temp\\go-build882050150" -w -extld=gcc -buildmode=exe -buildid=0180ed6e175ea3e4bc497fc21fe0319a733a9c8e "C:\\Users\\User\\AppData\\Local\\Temp\\go-build882050150\\command-line-arguments.a"
$WORK\command-line-arguments\_obj\exe\main.exe

After that it just sits there and dose nothing. Doesn't matter what program I run it will always hang forever.

No idea where to go from here. I don't really develop on Windows but I was trying to work with cross-compilation on Ubuntu and having run into this issue with the cross-compiled binary I thought I'd try compiling directly on Windows. But it appears my Windows just doesn't like Golang compiled binaries.

Go version is 1.5.1 windows/amd64.

Here's the output of go env:

set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=E:\go
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GO15VENDOREXPERIMENT=
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1

答案1

得分: 14

Avast表现得很愚蠢。禁用Avast后,所有程序都可以正常运行...

当然,让Avast保持禁用状态并不理想,所以我必须找到一种方法来排除go文件。不幸的是,除非为二进制文件设置单独的排除项,否则无法排除go编译文件本身。但是,排除go特定目录是可行的。

最终,我排除了go的安装位置,对我来说是C:\Go,以及GOPATH的目录,对我来说是E:\go

这在大部分情况下都有效,只要二进制文件是从这两个目录中运行的。但是,我在使用go run命令时仍然遇到问题。通过使用go run -x命令,我找到了Go放置临时编译二进制文件的位置。这个位置是C:\Users\username\AppData\Local\Temp\go-build1234\。然而,每次我使用go run时,go都会创建一个新的目录,所以我必须在Avast中创建一个排除项,使用两个通配符设置为C:\Users\username\AppData\Local\Temp\go-build*\*。现在程序可以正常运行了。

这些是我最终得到的排除项:
在Windows 10上出现Go程序挂起的问题。

英文:

Avast was being dumb. Disabled Avast and the programs all worked....

Of course leaving Avast disabled is not ideal so I had to figure out a way to exclude go files. Unfortunately there's no way to exclude go compiled files themselves, unless you set up individual exclusions for the binaries. However excluding go specific directories will work.

I ended up excluding the installation location of go for me this was C:\Go and the directory of GOPATH E:\go for me.

This worked for the most part as long as binaries were run from either of those two directories, but I was still having issues with using the go run command. Using the go run -x command I was able to find out where Go was putting the temporary compiled binaries. This ended up being at C:\Users\username\AppData\Local\Temp\go-build1234\. However go would create a new directory each time I used go run so I had to create an exclusion on Avast with two wildcards set C:\Users\username\AppData\Local\Temp\go-build*\*. Now programs will run correctly.

These are the exclusions I ended up with:
在Windows 10上出现Go程序挂起的问题。

答案2

得分: 3

问题在于,如果未设置GOTMPDIR,golang有时会在那个通用的“Temp”目录中进行构建,而这对于将其作为Kaspersky的通用例外是危险的。对于我来说,Kaspersky不允许在目录例外中使用通配符,就像Avast一样。

假设您已经将您的GOPATH添加为您的杀毒软件的例外(例如,“C:\Users<user>\go”...对我而言,以下操作有效:
1)在该路径中添加一个新目录(我将其命名为“tmp”,但您可以使用其他名称),即“C:\Users<user>\go\tmp”。
2)将golang环境变量GOTMPDIR设置为该目录...

go env -w GOTMPDIR="C:\Users<user>\go\tmp"

问题就解决了。但请注意,我已经将“C:\Users<user>\go”添加为我的杀毒软件的例外。

英文:

The problem is that if GOTMPDIR is not set, golang will sometimes build in that generic \Temp directory that is dangerous to allow as a generic exception to Kaspersky. And for me, Kaspersky doesn't allow wildcards in the directory exceptions like Avast

Assuming you've already added your GOPATH as an exception in your antivirus (for example, "C:\Users&lt;user>\go"...what worked for me was to

  1. add a new directory (I named mine "tmp" but you could probably name it a number of things) within that path, i.e., "C:\Users&lt;user>\go\tmp"
  2. Set the golang environment variable GOTMPDIR to it...
    > go env -w GOTMPDIR="C:\Users&lt;user>\go\tmp"

And the issue goes away. But note that I already added "C:\Users&lt;user>\go" as an exception to my antivirus.

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

发表评论

匿名网友

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

确定