當我使用gormigrate編寫數據庫遷移時,我需要在函數范圍內的兩個結構之間定義多對多關系。但在 golang 1.19 或 1.18 以下將無法編譯package mainimport "fmt"func main() { type Student struct { Courses []*Course // [Error] ./prog.go:7:14: undefined: Course } type Course struct { Students []*Student } fmt.Printf("This won't compile")}然而,將定義移到函數之外就可以了package mainimport "fmt"type Student struct { Courses []*Course}type Course struct { Students []*Student}func main() { fmt.Printf("This works")}可以在https://go.dev/play/p/GI53hhlUTbk上自己嘗試為什么會這樣?我怎樣才能讓它在功能范圍內工作?有沒有類似C++中typedef的語法,可以先聲明一個struct,再定義?
添加回答
舉報
0/150
提交
取消