主包中未识别导入的更新内容

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

Updates to import not recognized in main package

问题

我正在进行一个项目,有几个我创建的包,我正在将它们导入到我的主项目中。

现在,当我将代码移到一个外部包中时,我可以从主项目中正常调用它的函数。但是,一旦我在外部包中添加新的函数,构建过程中总是会出现“undefined func...”的错误。

我尝试重新构建我的外部包,但没有成功。我唯一能够更新对自定义包的引用的方法是将外部包和文件夹重命名。

例如,如果我在 /project/blah/blah.go 中有以下内容:

package blah

func DoSomethingCool(s string) {
	fmt.Print(s)
}

我可以在我的主应用程序(project/web/main.go)中调用这个函数,如 blah.DoSomethingCool("Hello World")。现在这个工作正常。但是,如果我将包 blah 更新为:

package blah

func DoSomethingCool(s string) {
	fmt.Print(s)
}

func DoSomethingElseEvenCooler(p string, q string) {
	fmt.Print(
		fmt.Sprint(p, q),
	)
}

我无法调用 blah.DoSomethingElseEvenCooler("p", "q"),即使重新构建了 blah。我得到了一个函数未定义的错误。

然而,如果我将 blah 重命名为 foo,并将路径重命名为 /project/foo/foo.go,我可以完全正常地调用 foo.DoSomethingElseEvenCooler("p", "q")。似乎包 blah 的引用没有被更新,即使它已经改变。在构建主项目时,有没有一种强制更新的方法?

或者我可能漏掉了什么?

英文:

I'm working on a project and have a couple of packages that I've created which I'm importing in my main project.

Now when I take code and move it into an external package I can call it's functions fine from my main project. But once I add new functions to my external package, I always get an undefined func... error during the build process.

I've tried rebuilding my external package.. but no luck. The only way I've been able to update the reference to my custom package, has been to rename the external package & folder.

For instance, If I have the following in /project/blah/blah.go:

package blah

func DoSomethingCool(s string) {
	fmt.Print(s)
}

I can call this func in my main application (project/web/main.go) as blah.DoSomethingCool("Hello World"). Now this works as expected. But now if I update package blah to:

package blah

func DoSomethingCool(s string) {
	fmt.Print(s)
}

func DoSomethingElseEvenCooler(p string, q string) {
	fmt.Print(
		fmt.Sprint(p, q),
	)
}

I cannot call blah.DoSomethingElseEvenCooler("p", "q") even with a rebuild of blah. I end up with a func undefined error.

However if I rename blah to foo and rename the path to /project/foo/foo.go, I can call foo.DoSomethingElseEvenCooler("p", "q") perfectly fine. Seems like the reference to package blah is not being updated even though it has changed. Is there a way to force this while I'm building my main project?

Or am I just missing something?

答案1

得分: 0

呼...在外部包上运行

go install

解决了我的问题。谢谢!

英文:

Phew... Running

go install

on the external package fixed my issues. Thanks!

huangapple
  • 本文由 发表于 2015年1月15日 11:25:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/27956345.html
匿名

发表评论

匿名网友

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

确定