"main redeclared in this block" see details

Hey guys, I have been learning Go for a few days now, and I stumbled across this error while coding:

main redeclared in this block (see details) 

I’m not sure what it means, but this is my code:

package main

import "fmt"

func main() { // Where error is, specifically main()
	fmt.Println(1 + 1)
	fmt.Println(1 - 1)
	fmt.Println(1 * 200)
	fmt.Println(200/20)

	fmt.Println(true || false)
	fmt.Println(!true)
	fmt.Println(true && false)
}

If anyone could help me with this, it would be great! Thanks so much in advance!

Do you have another .go files in your folder ? If so, check if you create a main() function in each of those files…

You can also use Go Playground ( Go Playground - The Go Programming Language ) just to check your main function…

1 Like

Yes, I do have another file in the same directory with the main() function. Why does this affect go at all?

Thanks so much! You are a big help!

Go operates on packages (usually one folder is one package) and all files in one package are in the same scope. It is as if all the code from all files in a single folder would be running in the same file. So every function, variable, type and so on needs to have a unique name in your package.

If you want to create one big folder with all your test examples in them (for example like me, I like to have one GitHub repo with all my test files in it), the best solution is to create multiple subdirectories within that folder. In each subdirectory, you can place you example code, but you leave the top level folder empty.

For example:

– Top level of repository –> do not place your .go files in here

–- example1 –> place one .go file here

– example 2 –> place one .go file here

– etc….

In this example, the only reason for having multiple .go files in one subdirectory is if they have a different package. So for example in your package main you also use a second package (sort of a library), to keep the main package clean.

Barring some exceptions like _test packages, multiple files in the same directory will be the same package. If you want to have a bunch of examples in subfolders, I often create a go module in the parent folder and then run them with relative paths. I wrote about this recently on another question here.

From the docs:

Multiple programs in the same repository will typically have separate directories:

project-root-directory/
  go.mod
  internal/
    ... shared internal packages
  prog1/
    main.go
  prog2/
    main.go

In each directory, the program’s Go files declare package main. A top-level internal directory can contain shared packages used by all commands in the repository.

Users can install these programs as follows:

$ go install github.com/someuser/modname/prog1@latest
$ go install github.com/someuser/modname/prog2@latest

So for example check out the example code from the book The Go Programming Language. Try the following:

# Clone that repo (note how many subfolders it has)
git clone https://github.com/adonovan/gopl.io.git
# Switch to newly-cloned folder
cd gopl.io
# Run one of the examples using relative path
go run ./ch1/helloworld
# Can also run with module name in go.mod:
go run gopl.io/ch1/helloworld