Hello,
I’m having an issue with circular imports in my project. So, let’s say I have two files user.go
and post.go
that are responsible for handling their respective model.
The user.go
file looks like this.
package model
type User struct {
Email string
Password string
FirstName string
LastName string
}
and the post.go
like this:
package model
import (
user "github.com/me/go-blog/src/user"
)
type Post struct {
Title string
Body string
Author user.User
}
The above works perfectly fine, however, I want to then define a slice of Post
as a property of User
like this.
package model
import (
post "github.com/me/go-blog/src/post"
)
type User struct {
Email string
Password string
FirstName string
LastName string
Posts []post.Post
}
Which of course results in a circular import because Post
uses User
.
Am I doing something fundamentally wrong? If so what is the typical approach to achieve what I’m trying to achieve?
Thanks!