-2

Please help me to understand why duplicate "func main" in "package main" is wrong. Error in VC: "main redeclared in this block".


// $ tree
// .
// ├── main.go
// ├── second.go

// ```go build main.go```
// or 
// ```go build .```


// file: main.go
package main

import (
    "fmt"
)

func main() {
    fmt.Println("this is file MAIN")
}

// file: second.go
package main

import (
    "fmt"
)

func main() {
    fmt.Println("this is file SECOND")
}

I can build/run this: go build/run main.go - correct go build/run . - wrong

Roman
  • 49
  • 2
  • 6
  • Possible duplicate of [How can I "go run" a project with multiple files in the main package?](https://stackoverflow.com/questions/28081486/how-can-i-go-run-a-project-with-multiple-files-in-the-main-package) – jk2K Sep 26 '19 at 01:24

2 Answers2

7

You can't have the same symbol declared twice at the package level in the same package.

And if you have 2 files in the same folder, both having package main declaration, that is exactly what you're doing. This is why your IDE complains: it tries to build / compile those 2 files as one package, same as writing go build .: this designates the package in the current folder, including all source files.

go run main.go and go run second.go works because you specify files to build (more specifically a single file), not packages. And having a single main.go or second.go being the main package does not violate the above rule: each file only contains the main() function once.

So in short: go run main.go ignores second.go.

Usually if you want to create multiple apps with multiple main() functions in the same project, it's easiest to just place different main() functions in different folders, conventionally inside a cmd folder.

See Command Go:

Compile packages and dependencies

Usage:

go build [-o output] [-i] [build flags] [packages]

Build compiles the packages named by the import paths, along with their dependencies, but it does not install the results.

If the arguments to build are a list of .go files, build treats them as a list of source files specifying a single package.

Compile and run Go program

Usage:

go run [build flags] [-exec xprog] package [arguments...]

Run compiles and runs the named main Go package. Typically the package is specified as a list of .go source files, but it may also be an import path, file system path, or pattern matching a single known package, as in 'go run .' or 'go run my/cmd'.

Also see What does go build build?

icza
  • 389,944
  • 63
  • 907
  • 827
  • Thank you for your reply. I can not find documentation about this case. A phrase like this: you can use both possibilities, but it is recommended ... – Roman Apr 15 '19 at 08:14
0

In Go,a directory is a package, and a package can only have one func with a given name(exception of a special case that is init()) Think of all your .go files in a directory as separate, but Go does not, it sees as a single package and that package declares multiple functions called main, which is not permitted.

Separating them(the main() func) into their own directories is the solution.