I have some code which consist of two packages, a api package and a student package (this is a hypothetical example to illustrate something I am having when reorganizing my current codebase) .
The student package contains logic to connect to a database, update student grades, fetch their grades, add students, remove students, and so on.
The api package implements an API and uses functions from the student package. So /addstudent in the api package may call AddStudent() in the student package.
In the api package I have a struct that represents a JSON request I expect to receive , and then that gets passed to the student package at one point.
// from api package
type StudentCreationRequest struct {
name string
age int
}
But in the student package I also have a struct that represents a student , and that may get returned and used in the api package.
// From student package
type Student struct {
name string
age int
grade int
... bunch of other stuff
}
Of course, this results in a circular depends and nothing will compile.
Is there a best practice to deal with packages importing structs from each other? Maybe making a dedicated package for the structs could work, but I don't know if that is best practice