亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Golang 中導入本地包

如何在 Golang 中導入本地包

Go
暮色呼如 2022-10-17 16:00:38
我有個問題。我無法在我的應用程序中導入本地包。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"`}
查看完整描述

1 回答

?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

我為您的場景創建了一個示例結構,如下所示:


假設項目結構如下所示:


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.


查看完整回答
反對 回復 2022-10-17
  • 1 回答
  • 0 關注
  • 166 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號