#How to run my go project when I have many `main.go` files in my directory tree?

8 messages · Page 1 of 1 (latest)

near grail
#

Hey everyone! I currently have this project structure:

.
├── cmd
│   └── importer
│       └── import.go
├── go.mod
├── go.sum
├── main.go

The question is: when I run go build, how can I run the import.go file instead of my main.go file in the root dir (and vice versa). I guess the main point is, how to select which file will be executed

atomic chasm
#

AFAIK you can go run path/to/file.go

winter rapids
#

you can also use package path
go run <mod name>/cmd/importer

near grail
#

Yeah, I know that. What I wanted to know is how all of this works after I run go build

#

Do I have to build one binary for each command?

winter rapids
#

yes, you just need to replace go run with go build.
the binary will be generated in CWD. you can use -o to specify the output.

tacit vector
#

it's enabled to build nested main.go.
mkdir bin <-- to hold executable binary
cd cmd/importer
go build -o ../../bin
the binary built inside /bin

somber fog
# near grail Hey everyone! I currently have this project structure: ```go . ├── cmd │   └── i...

go build ./cmd/importer

it will generate a binary named importer in the cwd by default. To change it, either because you want another name, or because that would be a conflicting name in the root directory use the -o flag

to build everything in the cmd dir use go build -o path_where_binaries_will_go ./cmd/...
to do the same for the whole project go build -o path_where_binaries_will_go ./...

in the latter 2 cases the -o flag is actually required as you are building multiple binaries