【问题标题】:I can't run Go programs anymore我不能再运行 Go 程序了
【发布时间】:2014-11-16 08:58:04
【问题描述】:

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

今天我才意识到我不能再运行 Go 程序了。我可以使用“go test”成功构建和运行单元测试。但是,运行任何已编译的 Go 程序(甚至是 hello world)会导致出现标题为“不支持的 16 位应用程序”的弹出窗口。错误信息如下:

此文件的版本与 Windows 的版本不兼容 你在跑。检查计算机的系统信息以查看 无论您需要 x86(32 位)还是 x64(64 位)版本的 程序,然后联系软件发行商。

无论我使用什么版本的 Go (x86/x64),结果都是一样的。另请注意,我没有使用任何 IDE。我从命令行调用 go.exe 来构建/测试。

我无法理解这一点,因为运行“go test”效果很好。

有什么想法吗?

编辑:

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

build/run output

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

C:\Program Files (x86)\Microsoft Visual Studio 11.0>dumpbin /headers
C:\Projects \GoPlayground\src\playground\playground.exe Microsoft (R)
COFF/PE Dumper Version 11.00.51106.1 Copyright (C) Microsoft
Corporation.  All rights reserved.


Dump of file C:\Projects\GoPlayground\src\playground\playground.exe

File Type: LIBRARY
C:\Projects\GoPlayground\src\playground\playground.exe : warning
LNK4003: invali d library format; library ignored
C:\Projects\GoPlayground\src\playground\playground.exe : warning
LNK4048: Invali d format file; ignored

  Summary


C:\Program Files (x86)\Microsoft Visual Studio 11.0>

这是完整的源代码:

package playground

import "fmt"
import "playground/another"

func main() {
    fmt.Println("Hello world!")
    fmt.Println(another.Foobar(2))
}

-------------------

package another

func Foobar(i int) int {
    return i + 1
}

EDIT2:

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

【问题讨论】:

  • 向我们展示您的工作,例如用一个简单的 hello world 显示你运行了哪些命令,你编译的文件在哪里以及任何其他感兴趣的东西,例如 `go -version 的输出
  • 向我们展示一个可重现的示例:How to create a Minimal, Complete, and Verifiable example
  • 好的,我刚刚添加了更多信息。
  • 如果重启系统,问题还会继续吗?
  • 是的,确实如此。我认为 dumpbin 的输出确实有问题。我只是不知道如何调试构建过程。

标签: go


【解决方案1】:

The Go Programming Language Specification

Program execution

通过链接单个未导入的包来创建完整的程序 用它导入的所有包调用主包, 及物的。主包必须有包名 main 并声明 一个不带参数且不返回值的函数 main。

func main() { … }

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

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

playground.go:

package main

import (
    "fmt"
    "playground/another"
)

func main() {
    fmt.Println("Hello world!")
    fmt.Println(another.Foobar(2))
}

playground/another.go:

package another

func Foobar(i int) int {
    return i + 1
}

输出:

你好世界! 3

【讨论】:

  • 这确实是问题所在。我记得几天前将 main 重命名为 Playground。我从没想过它会产生如此戏剧性的效果。谢谢!
猜你喜欢
  • 2016-02-10
  • 2021-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-29
  • 1970-01-01
相关资源
最近更新 更多