我无法再运行Go程序了。

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

I can't run Go programs anymore

问题

这是我遇到的最奇怪的问题。我在一个 Windows 2008 R2 虚拟机上设置了我的 Go 开发环境。我甚至没有重新启动它,也没有运行 Windows 更新。

今天我才意识到我无法再运行 Go 程序了。我可以成功地构建和运行单元测试,使用 'go test' 命令。然而,运行任何编译后的 Go 程序(即使是 hello world)都会弹出一个名为 'Unsupported 16-bit application' 的窗口。错误信息如下:

此文件的版本与您正在运行的 Windows 版本不兼容。请检查您计算机的系统信息,查看您是否需要 x86(32 位)或 x64(64 位)版本的程序,然后联系软件发布商。

无论我使用哪个版本的 Go(x86/x64),结果都是一样的。还请注意,我没有使用任何集成开发环境(IDE)。我是通过命令行调用 go.exe 进行构建和测试的。

我无法理解这个问题,因为运行 'go test' 是正常的。

有什么想法吗?

编辑:

这是我构建和运行程序时的控制台输出:

build/run output

有趣的是,dumpbin 表明可执行文件确实存在问题

  1. C:\Program Files (x86)\Microsoft Visual Studio 11.0>dumpbin /headers
  2. C:\Projects \GoPlayground\src\playground\playground.exe Microsoft (R)
  3. COFF/PE Dumper Version 11.00.51106.1 Copyright (C) Microsoft
  4. Corporation. All rights reserved.
  5. Dump of file C:\Projects\GoPlayground\src\playground\playground.exe
  6. File Type: LIBRARY
  7. C:\Projects\GoPlayground\src\playground\playground.exe : warning
  8. LNK4003: invali d library format; library ignored
  9. C:\Projects\GoPlayground\src\playground\playground.exe : warning
  10. LNK4048: Invali d format file; ignored
  11. Summary
  12. C:\Program Files (x86)\Microsoft Visual Studio 11.0>

这是完整的源代码:

  1. package playground
  2. import "fmt"
  3. import "playground/another"
  4. func main() {
  5. fmt.Println("Hello world!")
  6. fmt.Println(another.Foobar(2))
  7. }
  8. -------------------
  9. package another
  10. func Foobar(i int) int {
  11. return i + 1
  12. }

编辑2:

我已经重新安装了两次 Go,但没有效果。

英文:

This is the oddest problem I've ever encountered. I have my Go development environment set up on a Windows 2008 R2 virtual machine. I don't even restart it nor run Windows update.

Today I just realized that I can no longer run Go programs. I can successfully build and run unit tests with 'go test'. However, running any compiled Go program, (even hello world) causes a pop-up window titled 'Unsupported 16-bit application' to appear. The error message is as follows:

> The version of this file is not compatible with the version of Windows
> you're running. Check your computer's system information to see
> whether you need an x86 (32-bit) or x64 (64-bit) version of the
> program, and then contact the software publisher.

The result is the same regardless of what version of Go I use (x86/x64). Also note that I'm not using any IDE. I call go.exe to build/test from the command line.

I can't get my head around this since running 'go test' works just fine.

Any thoughts?

EDIT:

Here's the console output when I build and run the program:

build/run output

Interestingly, dumpbin suggests that indeed there's something wrong with the executable

  1. C:\Program Files (x86)\Microsoft Visual Studio 11.0>dumpbin /headers
  2. C:\Projects \GoPlayground\src\playground\playground.exe Microsoft (R)
  3. COFF/PE Dumper Version 11.00.51106.1 Copyright (C) Microsoft
  4. Corporation. All rights reserved.
  5. Dump of file C:\Projects\GoPlayground\src\playground\playground.exe
  6. File Type: LIBRARY
  7. C:\Projects\GoPlayground\src\playground\playground.exe : warning
  8. LNK4003: invali d library format; library ignored
  9. C:\Projects\GoPlayground\src\playground\playground.exe : warning
  10. LNK4048: Invali d format file; ignored
  11. Summary
  12. C:\Program Files (x86)\Microsoft Visual Studio 11.0>

And here's the full source code:

  1. package playground
  2. import "fmt"
  3. import "playground/another"
  4. func main() {
  5. fmt.Println("Hello world!")
  6. fmt.Println(another.Foobar(2))
  7. }
  8. -------------------
  9. package another
  10. func Foobar(i int) int {
  11. return i + 1
  12. }

EDIT2:

I've reinstalled Go twice with no effect.

答案1

得分: 38

《Go编程语言规范》

程序的执行

通过将一个名为main的单个未导入包与其导入的所有包进行链接,可以创建一个完整的程序,这是一个主要的包。主包必须具有包名main,并声明一个不带参数且不返回值的main函数。

  1. func main() { ... }

程序的执行从初始化主包开始,然后调用main函数。当该函数调用返回时,程序退出。它不会等待其他(非主)goroutine完成。

使用package main,而不是package playground。例如,

playground.go:

  1. package main
  2. import (
  3. "fmt"
  4. "playground/another"
  5. )
  6. func main() {
  7. fmt.Println("Hello world!")
  8. fmt.Println(another.Foobar(2))
  9. }

playground/another.go:

  1. package another
  2. func Foobar(i int) int {
  3. return i + 1
  4. }

输出:

  1. Hello world!
  2. 3
英文:

> The Go Programming Language Specification
>
> Program execution
>
> A complete program is created by linking a single, unimported package
> called the main package with all the packages it imports,
> transitively. The main package must have package name main and declare
> a function main that takes no arguments and returns no value.
>
> func main() { … }
>
> Program execution begins by initializing the main package and then
> invoking the function main. When that function invocation returns, the
> program exits. It does not wait for other (non-main) goroutines to
> complete.

Use package main, not package playground. For example,

playground.go:

  1. package main
  2. import (
  3. "fmt"
  4. "playground/another"
  5. )
  6. func main() {
  7. fmt.Println("Hello world!")
  8. fmt.Println(another.Foobar(2))
  9. }

playground/another.go:

  1. package another
  2. func Foobar(i int) int {
  3. return i + 1
  4. }

Output:

<pre>
Hello world!
3
</pre>
1: http://golang.org/ref/spec
2: http://golang.org/ref/spec#Program_execution

huangapple
  • 本文由 发表于 2014年11月16日 07:00:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/26951762.html
匿名

发表评论

匿名网友

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

确定