猿问

如何在 Golang 中导入本地包

我有个问题。我无法在我的应用程序中导入本地包。



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"`

}


暮色呼如
浏览 194回答 1
1回答

慕沐林林

我为您的场景创建了一个示例结构,如下所示:假设项目结构如下所示: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.goStep1:- 初始化模块go mod init project-villa或者go mod init github.com/user-name/project-villamod 将管理模块依赖本身。无论如何,如果没有,您可以显式导入它。它看起来像这样: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.
随时随地看视频慕课网APP

相关分类

Go
我要回答