导入并重用来自不同包的现有结构体。

huangapple go评论88阅读模式
英文:

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"

huangapple
  • 本文由 发表于 2021年8月18日 17:07:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/68829491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定