找到了包,但内容找不到?

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

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).

huangapple
  • 本文由 发表于 2014年11月16日 18:47:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/26956107.html
匿名

发表评论

匿名网友

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

确定