Program executable does not run - go

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")
}

Should be package main:
package main
import "fmt"
func main() {
fmt.Println("hello")
}
https://tour.golang.org/welcome

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)

Related

Difference between go run ./path/dir and go run path/dir

Note: This is not asking Why the functions defined in other "main" packages are not recognised?.
I've got a project with the following structure:
go/src/github.com/me/project/cmd/web/main.go
# main.go
package main
import(
"github.com/me/myproject/internal/myproject"
)
void main() {
// ...
}
go/src/github.com/me/project/cmd/web/handlers.go
# handlers.go
package main
void someFunc() {
// ...
}
And I compile and run successfully with:
go run ./cmd/web
I can also run successfully with go run cmd/web/*.go
However, when I try to run with go run cmd/web, why does compilation fail with the output:
package cmd/web: package cmd/web is not in GOROOT (/usr/local/go/src/cmd/web)
Why does ./cmd/web work, but cmd/web does not?
When you run ./cmd/web, you refer to the correct directory in your current directory tree.
cmd/web however refers to a package living where the stdlib packages are. If you try to run this example
go run net/http
It will tell you that it can't run it because its not a main package. It has found the stdlib net/http package with that name; but since cmd/web doesn't exist it doesn't work

Incorrect package name not throwing error on build

Considering the sample hello world with the wrong package name with the file named as main.go
package test
import "fmt"
func main() {
fmt.Println("hello world")
}
on go build main.go the build doesn't work (doesn't generate the executable) because of the incorrect package name. But why no error is thrown?
A package name test is not incorrect, it is valid according to Spec: Package clause:
PackageClause = "package" PackageName .
PackageName = identifier .
test is a valid Go identifier.
As to what does go build main.go do?
Generally you list packages to go build, but you may also list .go source files, just as in your example.
Quoting from go help build:
If the arguments to build are a list of .go files, build treats them as a list of source files specifying a single package.
So go build simply built your test package, consisting of a single main.go source file. It is not an error to add a main() function to a package that is not main.
As to why "nothing" happens: go build generates no output if everything is ok, and it outputs error if something is not right. go build applied on a non-main package just "checks" if the package can be built, but it discards the result. Please check What does go build build?

Why open the go build's binary file with vim, I can see some source repositories info?

In windows, new go file: test.go
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello World!")
}
Then run go build test.go and then vim test.exe.
Search test.go, I can see many dir infos.
Why it happens and how to hide the info?
Why it happens ?
The golang is a compiled language - that's mean it uses compiler (translators that generate machine code from source code). The test.exe file is a source code compiled into machine code.
How to hide the info?
The binary file (compiled program) contains all data(the source code, the .data section, strings and so on), you can't hide that info from binary, there is no way to do that, all that you can do is obfuscate somehow your source code.

Struct from the same parent folder as main is not visible

I have a small go demo project in Gogland with the structure:
awsomeProject
->src
->awsomeProject
->configuration.go
->main.go
Configuration file has a simple structure just for demo:
configuration.go:
package main
type Config struct {
Data int
}
Main file just uses the Config struct:
main.go
package main
import "fmt"
func main(){
var cfg Config
cfg.Data = 1
fmt.Println("lalala")
}
The error that I have is:
/usr/local/go/bin/go run /Users/lapetre/Work/awsomeProject/src/awsomeProject/main.go
command-line-arguments
src/awsomeProject/main.go:6: undefined: Config
Process finished with exit code 2
Any idea why the Config is not seen in main?
Thanks
When you build reusable pieces of code, you will develop a package as a shared library. But when you develop executable programs, you will use the package “main” for making the package as an executable program. The package “main” tells the Go compiler that the package should compile as an executable program instead of a shared library. The main function in the package “main” will be the entry point of our executable program.
That's why you should use the following structure:
awsomeProject
->src
->awsomeProject
->configuration.go
->main.go
with main.go
package main
import "fmt"
func main(){
var cfg awsomeProject.Config
cfg.Data = 1
fmt.Println("lalala")
}
and configuration.go
package awsomeProject
type Config struct {
Data int
}
For more details:
https://golang.org/doc/code.html
https://thenewstack.io/understanding-golang-packages
How are you calling go run? If you're calling it like
go run main.go
then that's the problem.
Go run only runs the file(s) you tell it to. So you need to tell it to also run configuration.go, or if you have several go files to run you can use
go run *.go
as eXMoor suggested.
There are some limits/drawbacks to "go run *.go" however, so the better alternative is to use go build instead of go run.
go build
Will compile everything. Then, to run the executable:
./awesomeProject
To combine all of this into one command that will compile and run whatever app you're working on, you can use:
go build && ./${PWD##*/}
I have it aliased to
gorunapp
just to make it easier.
This may not actually be the answer to the problem you're having, but hopefully you'll find it useful information regardless.
Try to run your application with command:
go run *.go

How to use multiple .go files in the same application

Good Morning All,
I am new to Golang. I want to move some of my functions out into separate files so that I will not have like a 10,000 line .go file at the end. lol. I created two files both have the same package called main. Do I need to change package name to be app specific? Anyway how do I get these two files to talk?
Example:
MainFile.go:
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello World!")
Test()
}
NewFile.go:
package main
import (
"fmt"
)
func Test() {
fmt.Println("Hello World Again!")
}
The test method is in the second file but cannot be reached by the first. I am sure this is some rudimentary thing I am missing.
Thanks
Update:
I tried specifying this on the command line: go build MainFile.go NewSourceFile.go. It comes back with no errors but never builds the binary. How do I get it to output the binary now?
If you run go run MainFile.go, Test() won't be found because its not in that file. You have to build the package then run the package:
Inside the folder where the 2 files are, run go build and you will get a binary in that folder. Then just run the binary: ./MyPackage

Resources