英文:
Package is found but contents are not?
问题
我在构建我的Go项目时遇到了一个奇怪的错误。
我的目录结构如下:
-$GOPATH
-src
-main
-main.go
-configuration
-configuration.go
configuration.go:
package configuration;
type Config int;
func (c Config) Parse(s string) map[string]string {...}
main.go
package main;
import "configuration";
func main() {
var config Config;
argMap := config.parse(...);
return;
}
如果我的工作目录是$GOPATH
,我执行以下操作:
go build configuration - 没有输出,正常
go build main
imported and not used "configuration"
undefined: Config
所以我的包被找到了($GOPATH/pkg
中包含了具有正确内容的configuration.go
- 我可以看到Parse
方法),而且main
导入了它,但是却无法识别它的内容?
我猜测问题可能是Config
类型没有被导出?为什么会这样呢?
英文:
I get a strange error while building my go project.
My structure:
-$GOPATH
-src
-main
-main.go
-configuration
-configuration.go
configuration.go:
package configuration;
type Config int;
func (c Config) Parse(s string) map[string]string {...}
main.go
package main;
import"configuration"
func main() {
var config Config;
argMap := config.parse(...);
return;
}
if my working directory is $GOPATH
, I do:
go build configuration - no output, OK
go build main
imported and not used "configuration"
undefined: Config
So my package is found ($GOPATH/pkg
contains configuration.go
with correct content - I can see the Parse
method) and main
imports it, but does not recognize its contents?
I recon the problem is that the type Config
is not exported? Why would that be?
答案1
得分: 2
你正在尝试使用main
包中未定义的Config
,而不是来自configuration
包的Config
(这就是错误信息"imported and not used"的原因):
package main
import "configuration"
func main() {
var config configuration.Config
argMap := config.Parse(...)
}
第二个问题是调用未公开的parse
而不是Parse
,正如VonC所解释的那样。
英文:
You are trying to use Config
from package main
, where it is not defined, instead of the one from configuration
(thats the error "imported and not used"):
package main
import "configuration"
func main() {
var config configuration.Config
argMap := config.Parse(...)
}
The second problem is calling unexported parse
instead of Parse
as explained by VonC.
答案2
得分: 0
argMap := config.parse(...)不会起作用,因为你声明了一个Parse()方法。
(就像"导出的方法configuration.Parse()
"一样)
var config configuration.Config
argMap := config.Parse(...);
Config是被导出的,但方法是区分大小写的(参见导出标识符)。
英文:
argMap := config.parse(...);
wouldn't work, since you declared a Parse()
method.
(as in "exported method configuration.Parse()
")
var config configuration.Config
argMap := config.Parse(...);
Config
is exported, but the methods are case-sensitive (cf. Exported Identifiers).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论