未定义:函数(在另一个包中声明)

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

undefined: function (declared in another package)

问题

你的项目组织结构如下:

  • GOPATH
    • src
      • cvs/user/project
        • main.go
        • utils
          • utils.go

main.go的代码如下:

package main

import (
  "fmt"
  "cvs/user/project/utils"
)

func main() {
   ...
   utilsDoSomething()
   ...
}

而utils.go的代码如下:

package utils
       
import (
  "fmt"
)   

func utilsDoSomething() {
    ...
}

编译器告诉我:

main.go导入但未使用:"cvs/user/project/utils"
main.go未定义:utilsDoSomething

我不知道我做错了什么。有任何想法都会有帮助,提前谢谢!

英文:

my project organisation looks like this :

  • GOPATH
    • src
      • cvs/user/project
        • main.go
        • utils
          • utils.go

main.go looks like this :

package main

import (
  "fmt"
  "cvs/user/project/utils"
)

func main() {
   ...
   utilsDoSomething()
   ...
}

and utils.go :

package utils
       
import (
  "fmt"
)   

func utilsDoSomething() {
    ...
}

The compiler tells me that :

main.go imported and not used: "cvs/user/project/utils"

main.go undefined: utilsDoSomething

I don't know what I'm doing wrong. Any idea would be helpful, thank you in advance !

答案1

得分: 10

你在main.go中忘记了包前缀,并且你的函数没有被导出,这意味着它无法从其他包中访问。要导出一个标识符,可以在名称开头使用大写字母:

utils.UtilsDoSomething()

一旦你有了utils前缀,你也可以在名称中省略Utils

utils.DoSomething()

如果你想将utils包中的所有内容导入到主应用程序的命名空间中,可以使用以下代码:

import . "cvs/user/project/utils"

之后你就可以直接使用DoSomething了。

英文:

You forgot the package prefix in main.go and your function is not exported, meaning it is not accessible from other packages. To export an identifier, use a capital letter at the beginning of the name:

utils.UtilsDoSomething()

Once you have the utils prefix you can also drop the Utils in the name:

utils.DoSomething()

If you want to import everything from the utils package into the namespace of your main application do:

import . "cvs/user/project/utils"

After that you can use DoSomething directly.

答案2

得分: 1

在Go语言中,以小写字母开头的符号(包括函数名)不会被导出,因此对其他包不可见。

将该函数重命名为UtilsDoSomething

如果你强烈反对导出该函数,并且你使用的是Go 1.5或更高版本,你可以通过将utils目录放在internal目录中,使该函数只对你的项目可见。

内部包的规范

英文:

In Go the symbols (including function names) starting with lower case letters are not exported and so are not visible to other packages.

Rename the function to UtilsDoSomething.

If you strongly oppose exporting this function and you are using Go 1.5 or later, you can make your function visible to only your project by placing utils directory inside internal directory.

Specification of internal packages

答案3

得分: 1

当你引用一个包中的函数时,你需要使用包名.函数名的形式来引用(首字母大写)。

在你的情况下,使用如下方式:

utils.UtilsDoSomething()

英文:

When you are referencing a function from a package you need to reference it using the package name.Function name (with capital case).

In your case use it as:

utils.UtilsDoSomething().

答案4

得分: 0

在包utils中,函数名也需要大写。

func UtilsDoSomething()
英文:

function name also needs to be capitalized in the package utils

func UtilsDoSomething()

huangapple
  • 本文由 发表于 2016年4月29日 19:22:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/36937076.html
匿名

发表评论

匿名网友

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

确定