为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 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 关注
  • 141 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号