11

I have a project in github.com/user called project:

src/
    github.com/user/project
        sub1/
            main.go
            sub1.exe (??)
        sub2/
            main.go
            sub2.exe (??)

I am trying to compile the package in my project. When I:

$ cd github.com/user/project/sub1
$ go build

Nothing happens. go build seems to finish without complaining, but there is no executable file. How can I build packages into executable?

"go version go1.3 windows/amd64"

2
  • 1
    If main.go has a main() function, it should build the executable and call it sub1.exe in Windows. Isn't sub1.exe what you are looking for? Remove it and run go build again to see if it is created. Commented Feb 8, 2015 at 17:01
  • Hm, there is no executable. I am in the directory of the package. I am using Windows, but building from cygwin commandline. But even when using Windows cmd, there is no executable. Commented Feb 9, 2015 at 6:38

1 Answer 1

11

It doesn't matter if you name your file main.go or shubudoo.go. The only thing what matters if you want to build an executable (a command) is that your files start with package main. One more thing: Go has absolutely no notion of "subpackage": All packages are equal for the compiler. The filesystem nesting is for your convenience only.

Sign up to request clarification or add additional context in comments.

5 Comments

So, it's not possible to have a main() in a package other than main?
The link is what I meant. It's a new main package in a project.
I have to admit I do not understand your question. Any package which is declared package main must have a main() function and the go tool will build such a package into an executable and the main() function will be called when you run the executable. Regarding camget: camget is such a package main package and it contains a main() function an it is built into an executable. It does not matter if other packages, other non-main-packages, completely unrelated stuff to Go or whatever lives in other directories (higher up, siblings or subfolders).
My misunderstanding was, that I thought I could build packages into executable binaries, if the packages provided a main(), regardless of whether the package is main or cmd or xxx. I thought the main() decided whether there is an executable.
No, it is package main and func main() { for an executable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.