The syntax of `go build` command

If my .go files (all files declared as main mackage) lays in the root of project folder, then
go build does nothing but threre are no errors in terminal

and if my .go files (all files declared as main mackage) lays in main folder, then if I run go build it produces executable in the main folder.

Whats wrong with first way of using go build?

2 Likes

Based on your description, I assume you have a folder structure like this:

/root/folder/
 |
 +- main/
     |
     +- main.go
     +- file0.go
     +- file1.go

And that while you’re in the main/ folder, go build works but in /root/folder/, it doesn’t.

The reason is that Go packages must all be in the same folder. When you run go build from /root/folder/, go build looks for .go files in that folder to build a package. go build does not traverse the directory structure from the current directory and build subpackages.

To build your main package from /root/folder/, run go build ./main from that folder. I think the program will still end up being called main, though.

3 Likes

Hi Sean, thank you.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.