I'm having trouble understanding what should be a trivial issue.
I have the following file structure:
.
├── go.mod
├── main.go
├── p1
│ └── p1.go
└── p2
└── p2.go
The files are small and simple.
-- go.mod
module wp
go 1.18
-- main.go
// main.go
package main
import (
"wp/p1"
"wp/p2"
)
func main() {
p1.P()
p2.P()
}
-- p1/p1.go
// p1.go
package p1
import (
"fmt"
"wp/p2"
)
func P() {
fmt.Printf("p1\n")
p2.P()
}
-- p2.go
// p2.go
package p2
import (
"fmt"
"wp/p1"
)
func P() {
fmt.Printf("p2\n")
p1.P()
}
In main.go I'm trying to call a function in packages p1 and p2. In package p1 p1.go is trying to call a function in package p2, and in package p2 p2.go is trying to call a function in package p1. Simple, right?
However, running "go build ." in the top level directory says:
package wp
imports wp/p1
imports wp/p2
imports wp/p1: import cycle not allowed
Is this because p1 imports p2, which imports p1, which imports p2, ...?
How can this program be made to work? I've tried many things, which all lead back
to the situation I describe above.
On a separate but similar note, I originally had another file in the top level directory that
contained a function I wanted to call from one of the packages. (I know this isn't a good idea, but I'm rewriting existing non-Go code into Go). This also failed to build for similar
reasons. Researching the issue showed several postings claiming that this wasn't
possible. Is this true?
Cordially,
Jon Forrest