我有個問題。我無法在我的應用程式中匯入本地包。
type Post struct {
URL string `json:"url,omitempty"`
Caption string `json:"caption,omitempty"`
Likes []User `json:"likes,omitempty"` // Can not import User from package user
}
type User struct {
Name string `json:"name,omitempty"`
Password string `json:"password,omitempty"`
Followers []User `json:"followers,omitempty"`
Followings []User `json:"followings,omitempty"`
}
uj5u.com熱心網友回復:
我為您的場景創建了一個示例結構,如下所示:
假設專案結構看起來像這樣:
project-villa/ //Name of your Project
model/
-user.go //this file will contain your User Structure
repository/
-post.go //this file will hold your Post structure and the rest piece of code
handler/
driver/
main.go
Step1:- 初始化模塊
go mod init project-villa
或者
go mod init github.com/user-name/project-villa
mod 將管理模塊依賴本身。無論如何,如果沒有,您可以顯式匯入它。它看起來像這樣:
github.com/random/project-villa/models
type Post struct {
URL string `json:"url,omitempty"`
Caption string `json:"caption,omitempty"`
Likes []models.User `json:"likes,omitempty"` //you can use it like this
}
參考go dev 官方鏈接。在這里,您將獲得Importing packages from your module.
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/373125.html
