BAR
BAR

Reputation: 17151

Program executable does not run

If I run go build -o bin/test ./scripts/test.go I get an executable, but when I try to run it I get

Failed to execute process './bin/test'. Reason:
exec: Exec format error
The file './bin/test' is marked as an executable but could not be run by the operating system.

scripts/test.go

package scripts

import (
    "fmt"
)

func main() {
    fmt.Println("hello")
}

Upvotes: 0

Views: 998

Answers (2)

Hymns For Disco
Hymns For Disco

Reputation: 8425

The documentation for the go build command has the details about how this works.

Summary:

  • Building "main" packages will output an executable file
  • Building a non-main package will output an object (and discard it)
  • Building with -o will force the command to not discard the output, and save it to the specified file

So basically what you're doing here is building a non-main package, which outputs outputs an object file, not an executable. Normally this is discarded, but due to using the -o option you've forced it to output it to a file.

To get an actual executable, you just need to change the package name to main (as noted by Steven Penny)

Upvotes: 1

Zombo
Zombo

Reputation: 1

Should be package main:

package main
import "fmt"

func main() {
   fmt.Println("hello")
}

https://tour.golang.org/welcome

Upvotes: 2

Related Questions