Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code: Go - main redeclared in this block [closed]

This warning/problem pops up in VS code;

main redeclared in this block compiler(DuplicateDecl) <my another Go file name in my directory> other declaration of main

I am have all my Go practice files in one directory and each file has a func main() block.

Is anyone familiar with the warning? if so how may I resolve.

But, Even though I get this error.. or what ever you call it..

I can still run my Go code in the terminal and see my input/output without any error.

like image 817
Santhosh Puvaneswaran Avatar asked Mar 04 '26 04:03

Santhosh Puvaneswaran


2 Answers

Separating them into their own directories is the only solution.

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

like image 89
Adrian Avatar answered Mar 05 '26 21:03

Adrian


At the end of the day, I resolved this problem by creating a directory for each Go file.

I know it is not a perfect solution for this problem..

If some one knows some thing please share with us.

like image 42
Santhosh Puvaneswaran Avatar answered Mar 05 '26 23:03

Santhosh Puvaneswaran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!