Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't run Go programs anymore

Tags:

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

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>

And here's the full source code:

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:

I've reinstalled Go twice with no effect.

like image 605
Alex G. Avatar asked Nov 15 '14 23:11

Alex G.


People also ask

How do I run a go program?

To run a Go program (assuming you have installed Go on your system), you need to instruct the Go compiler to compile and run a program using go run command with the relative or absolute path of the Go program file.

How do I run a go program on Windows?

To run a go program, create a file with an extension .go and write your golang code in that file. For example, we will create a file named helloworld.go and write the following code in it. Now open command prompt and navigate to the location of helloworld.go file.

Does go require a runtime?

Does Go have a runtime? Go does have an extensive library, called the runtime, that is part of every Go program. The runtime library implements garbage collection, concurrency, stack management, and other critical features of the Go language.

Why is go run so slow?

It is probably building dependencies. Try go install first to build your package and its dependencies. After that, go run should be much faster.


1 Answers

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:

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
}

Output:

Hello world!
3
like image 143
peterSO Avatar answered Oct 13 '22 20:10

peterSO