-3

I have multiple Go projects (and all of them are also Go modules) all in a folder. They are all HTTP servers and exchanging REST calls, thus, I need all of them up and running simultaneously.

So, for local testing purposes, I thought it would be reasonable to run all of them from the parent instead of moving all of the project root directories and running go run main.go in multiple terminals.

container_dir/
├── prj1/
│   ├── go.mod
│   ├── main.go
│   └── ...
├── prj2/
│   ├── go.mod
│   ├── main.go
│   └── ...
└── ...

Here are some some commands I have tried and the error messages for each time:

container_dir $ go run ./*/*.go
##ERROR: named files must all be in one directory; have ./prj1/ and ./prj2/
container_dir $ go run *.go
##ERROR: stat *.go: no such file or directory
container_dir $ go run ./prj1 ./prj2/
##ERROR: cannot find package "github.com/jackc/pgx/v4" in any of:
           /usr/local/go/src/github.com/jackc/pgx/v4 (from $GOROOT)
           /home/user/go/src/github.com/jackc/pgx/v4 (from $GOPATH)
         cannot find package ...

So, I can give a final rephase for the question: How to run multiple go modules in sibling directories when they have some third party dependencies etc.?

P.S: As possible with Go modules suggest container_dir for my projects is in an arbitrary location and I expect no $GOPATH relevance.

Go version: 1.13.6

vahdet
  • 6,357
  • 9
  • 51
  • 106
  • 1
    `go run` launches a single go app, not multiple. – icza Jan 16 '20 at 14:16
  • Ok, nice info. But still I cannot find a way even with separate `go run` calls without changing directory like `cd prj1` etc. – vahdet Jan 16 '20 at 14:27
  • `go run prj1/main.go` should launch the first app–given its main package consists of this single file. `go run prj2/main.go` should launch the other app. – icza Jan 16 '20 at 14:32
  • I am nit sure. It results in the app cannot find dependencies indeed like `cannot find package "github.com/jackc/pgx/v4"` so on. However, when I do `go run main.go` in `prj1` directory, it runs. – vahdet Jan 16 '20 at 14:38
  • 2
    What is wrong with `go build whatever && ./whatever` ? You are deliberately making your life complicated. – Volker Jan 16 '20 at 14:43
  • Would something like `ls -F | grep "/" | xargs -I % bash -c "go run %*.go"` work? (assuming you're using a bash shell and want to run them sequentially) – John Jan 17 '20 at 00:54

2 Answers2

4
  1. Don't use go run outside of tiny, playground-style tests
  2. Source paths are a compile-time concern and are irrelevant at runtime - being in "sibling directories" doesn't mean anything when the program is running
  3. go run runs a single program; just like building a program and running its executable binary (which is what go run does) runs a single program.
Adrian
  • 42,911
  • 6
  • 107
  • 99
  • Indeed I am not telling using a single `go run` for multiple apps is a must for my case. I was only brainstorming for running multiple apps at once without traversing through directories. – vahdet Jan 16 '20 at 14:43
-1

Looks like your go.mod stuff is having problems dude.

remember you can do replace inside it and reference your other application

module container_dir/prj2

go 1.13

require(
container_dir/prj1 v0.0.0
)

replace (
container_dir/prj1 => ../prj1
)

require is the path you import but it'll get switched to the relative path on build.

  • Thanks for the effort, but a module does not import from another. They are self-sufficient for compile-time. – vahdet Jan 16 '20 at 15:54