进口包装类型 [英] Import package & type

查看:79
本文介绍了进口包装类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题和我的项目结构

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:

  1. imported and not used.
  2. undefined: Config
  1. imported and not used.
  2. 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:

  1. 将config变量重命名为cfg
  2. 来自程序包config的引用配置,其限定名称为config.Config
  1. Rename the config variable to e.g. cfg
  2. Reference Config from package config with its qualified name config.Config

这篇关于进口包装类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆