Getting error while declaring packages

I am getting this error in golang 1.17.3: main.go:4:2: found packages routes (routes.go) and main (controller.go) in /home/developer/go/src/testapp

I am creating two different packages in same directory, i get this error but when i move them to different folders code works fine. Is there any change in go’s new version becuase it works fine on my another pc.

You can have multiple files in the same directory, but they need to have the same package name (they could both have package routes or they could both have package main, etc., but you cannot mix package names in the same folder).

But in 1.16 i use to declare different packages in Internal, cmd directory. It worked like a charm.

Do you think it has something to do with go 1.17.3 ?
Because in older versions it is working fine. @skillian

I have Go 1.16.5 and it doesn’t work there. When I try to put two files with different packages in the same folder and build, I get an error:

$ go build
found packages a (a.go) and b (b.go) in /home/sean/go/src/github.com/skillian/mypkg

Is there a repo that I could try cloning and testing where you have your mixed packages and it’s working?

1 Like

is there a ref doc somewhere that defines this behaviour?

EDIT: the language refspec is not strict on this behaviour
The Go Programming Language Specification - go.dev.

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