Can I create multiple main.go under one project?

in a real project I would create just one main.go, but now I’m learning go with many hands-on coding so I’d like to know if I can create multiple main.go under different folders so it’s convenient for me to learn all codings in one place?

in a real project, can I create multiple main.go files?
what’s the best practice?

thanks
James

2 Likes

Yes, thats totally fine. For go build or go run you then provide the path to the main file you want to compile.

2 Likes

Adding some topping to @Nobbz answer, you can refer to this project structure (it’s not a golang strict rule but as a reference): https://github.com/golang-standards/project-layout. You should interested in /cmd.

1 Like

yes but not on the same path (not in the same folder)

2 Likes

It’s possible to have them in the same folder. I personally do that slot, as I have a lot of folders which keep only a single file.

2 Likes

i use GoLand from JetBrains and when i try to add in the same folder two main.go files it gives me an error
“File already exists.”

2 Likes

Of course you can’t give them the same name when you put them in the same folder, but you can name them differently, as long as the all have a package main and a main() function.

2 Likes

Yes different file name is allowed as long as they are under the same package. I guess i didn’t got the question right, i think he asked about two main.go files in same folder (same name)
… sorry

2 Likes

Thanks for all the reply and sorry for my late response - busy with my daytime work.

I tried to create two go files under same folder, each file has package main and func main(),

in this folder, when I run go build, I get some complain like

(base) zwang-mac:cmd1 zwang$ go build
# _/Users/zwang/GolandProjects/learngo/cmd/cmd1
./race.go:14:6: main redeclared in this block
        previous declaration at ./main.go:10:6

and it dosen’t create any binary file. But when I do go build for each go file, like

go build main.go
go build race.go

it does generate binary files for each successfully. so I think my question is why go complains the go build without specifying the go file in this case

Thanks
James

2 Likes

The reason is that when there is no filename provided to go build, it treats the given command as building the package (go build .). The compiler can see both main.go and race.go at the same time and, it does not know which func main() you prefer to use. Hence, it said one of them is re-declared.

Go allows single script build which is why you can build each of them independently. However, it does not include dependencies so when you do that.

In practice, it is one folder for one package main with one func main(). You can create as many main folders as you want in a repository.

As a practice, you can package these main folders under a single, unified a directory called cmd/. Here’s an example: golangci-lint/cmd at master · golangci/golangci-lint · GitHub

1 Like

so, per my understanding, I can build project as bellow:

projects
-----cmd
----------cmd1
-----------------main.go
-----------------others.go
----------cmd2
-----------------main.go
-----------------others.go
....

is that right?
2 Likes

Yes, similarly. You can name cmd1 and cmd2 with the precise program name.

I would recommend you do something like this:

repo
  +----cmd
  |     +----- cmd1
  |     |        +------ main.go
  |     |
  |     +----- cmd2
  |              +------ main.go
  |
  +----others.go
  +----libs.go
  +---- ...

The idea is to keep the main packages as simple and clean as possible while having your functions and library codes in the repository. This way, your repo is still importable by any cmd main codes and externally.

When in doubt, feel free to explore the layout.

1 Like

I’m trying to understand However, it does not include dependencies so when you do that. so build a structure like this:

learngo
+-------cmd
|            +--------cmd2
|             |            +-----------main.go
+-------pkg
|             +--------pkg1
|              |            +----------addition.go

and here is my main.go

package main

import "fmt"
import "learngo/pkg/pkg1"

func main(){
	fmt.Println("hello")
	fmt.Println(pkg1.Addition(2, 3))
}

here is my addition.go:

package pkg1

func Addition(a int, b int) int{
	return a + b
}

under cmd/cmd2, I executed go build main.go, it generated binary file ‘main’, and when I run it it does print 5 as expected, so I think with go build main.go it looks like also include the dependence (which is the addition.go under pkg/pkg1 in this cae). is that right?

2 Likes

Yes, it does import pkg/pkg1 successfully inside your main. Excellent! :grin:

Do note that anything inside pkg can be on its own repository. Hence, you do not have to sqeeze everything inside a single repository.

1 Like

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