英文:
Import and reuse existing struct from different package
问题
我需要从位于vendor文件夹中的不同包中导入现有的结构体。我的文件夹结构如下:
projectname
custom
custom.go
vendor
github.com
somepackage
authentication
models.go
models.go包含以下结构体:
package authentication
type User struct {
ID int64
AccessToken string
Email string
FullName string
Password string
PasswordHash string
PasswordSalt string
Readonly bool
Role Role
Token string
Username string
}
现在,在custom/custom.go中,我有一个遵循特定签名的函数,代码如下:
签名
type loginFn func(string, string) (*User, error)
我这样导入:
import "github.com/somepackage/authentication/models"
func SetupGoGuardian(u, p string) (*User, error) {
但是我得到了错误"无法导入......(没有所需的模块提供包)"。
如何正确导入另一个包中的结构体并在自定义函数中使用它?
英文:
I need to import the existing struct from different package that lives in vendor folder. My folder stucture is below
projectname
custom
custom.go
vendor
github.com
somepackage
authentication
models.go
the models.go contains this struct
package authentication
type User struct {
ID int64
AccessToken string
Email string
FullName string
Password string
PasswordHash string
PasswordSalt string
Readonly bool
Role Role
Token string
Username string
}
Now inside my custom/custom.go, I have a function that follows a signature and the code looks like this
Signature
type loginFn func(string, string) (*User, error)
I import like this
import "github.com/somepackage/authentication/models"
func SetupGoGuardian(u, p string) (*User, error) {
But I got error coud not import ...... (no required module provides package)
How to properly import the struct from another package and use it a custom function?
答案1
得分: 2
你不能导入文件,只能导入包。将你的导入语句更改为引用包名:
import "github.com/somepackage/authentication"
英文:
You cannot import files, only packages. Change your import statement to reference the package name:
import "github.com/somepackage/authentication"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论