进口包装类型 [英] Import package & type
问题描述
这是我的问题和我的项目结构
Here is my problem and my project structure
src
|-->config
|--> config.go
|-->otherPackage
|--> otherFile.go
|-->main.go
我要在otherFile.go
但是当我尝试将其添加到导入中时,这些问题就出现了:
But when I tried to add it to the import here theses issues:
-
imported and not used.
-
undefined: Config
imported and not used.
undefined: Config
尽管我在函数声明中使用了它
Although I use it in the function declaration
function(target float64, entries [2]float64, config Config)
这是什么问题?
我尝试用
import (
"fmt"
"math"
"../config"
)
推荐答案
您不能从包中导入".您所能做的就是导入整个程序包".这意味着,如果您import "full/import/path/of/foo"
并且该程序包在开始时通过package foo
声明了自己的名称为foo,则该程序包中的所有内容都必须由foo
进行限定:
You cannot "import from a package". All you can do is "import the whole package". That means if you import "full/import/path/of/foo"
and that package declared itself to be called foo via package foo
at the beginning, then everything in this package has to be qualified by foo
:
foo.Config
如果您的软件包被称为config
,则比声明变量config
会使整个软件包更糟:因此,您必须:
If you package is called config
than declaring a variable config
will shaddow the whole package: so you have to:
- 将config变量重命名为
cfg
- 来自程序包config的引用配置,其限定名称为
config.Config
- Rename the config variable to e.g.
cfg
- Reference Config from package config with its qualified name
config.Config
这篇关于进口包装类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!