在Go语言中调用另一个包中的函数。

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

Call a function from another package in Go

问题

我有两个文件,main.go位于package main下,另一个文件中有一些函数,属于名为functions的包。

我的问题是:如何从package main调用一个函数?

文件1:main.go(位于MyProj/main.go)

package main

import "fmt"
import "functions" // 我在这里创建引用没有问题

func main(){
    c := functions.getValue() // <---- 这就是我想要做的
}

文件2:functions.go(位于MyProj/functions/functions.go)

package functions

func getValue() string{
    return "Hello from this another package"
}
英文:

I have two files main.go which is under package main, and another file with some functions in the package called functions.

My question is: How can I call a function from package main?

File 1: main.go (located in MyProj/main.go)

package main

import &quot;fmt&quot;
import &quot;functions&quot; // I dont have problem creating the reference here

func main(){
    c:= functions.getValue() // &lt;---- this is I want to do
}

File 2: functions.go (located in MyProj/functions/functions.go)

package functions

func getValue() string{
    return &quot;Hello from this another package&quot;
}

答案1

得分: 77

你可以通过导入路径导入包,并通过包名引用其所有导出的符号(以大写字母开头的符号),像这样:

import "MyProj/functions"

functions.GetValue()
英文:

You import the package by its import path, and reference all its exported symbols (those starting with a capital letter) through the package name, like so:

import &quot;MyProj/functions&quot;

functions.GetValue()

答案2

得分: 21

你应该在main.go中的导入语句前加上前缀MyProj,因为无论你是否将代码所在的目录称为main,在Go中,该目录默认是一个包名。它将被命名为MyProj

package main只是表示该文件包含一个可执行命令,其中包含func main()。然后,你可以通过go run main.go来运行此代码。更多信息请参见这里

你应该将functions包中的func getValue()重命名为func GetValue(),因为只有这样,该函数才能对其他包可见。更多信息请参见这里

文件1:main.go(位于MyProj/main.go)

package main

import (
    "fmt"
    "MyProj/functions"
)

func main(){
    fmt.Println(functions.GetValue())
}

文件2:functions.go(位于MyProj/functions/functions.go)

package functions

// `getValue`应该改为`GetValue`,以便对其他包可见。
// 它应该以大写字母开头。
func GetValue() string{
    return "Hello from this another package"
}
英文:
  • You should prefix your import in main.go with: MyProj, because, the directory the code resides in is a package name by default in Go whether you're calling it main or not. It will be named as MyProj.

  • package main just denotes that this file has an executable command which contains func main(). Then, you can run this code as: go run main.go. See here for more info.

  • You should rename your func getValue() in functions package to func GetValue(), because, only that way the func will be visible to other packages. See here for more info.

File 1: main.go (located in MyProj/main.go)

package main

import (
    &quot;fmt&quot;
    &quot;MyProj/functions&quot;
)

func main(){
    fmt.Println(functions.GetValue())
}

File 2: functions.go (located in MyProj/functions/functions.go)

package functions

// `getValue` should be `GetValue` to be exposed to other packages.
// It should start with a capital letter.
func GetValue() string{
    return &quot;Hello from this another package&quot;
}

答案3

得分: 10

导出函数getValue,将函数名的第一个字符改为大写,变为GetValue。

英文:

Export function getValue by making 1st character of function name capital, GetValue

答案4

得分: 5

你可以这样写:

import (
  functions "MyProj/functions"
)

func main() {
  c := functions.getValue() <-
}

如果你在gopath中写代码,可以使用以下导入语句:functions "MyProj/functions"。如果你正在使用 Docker 进行开发,可以使用相应的导入语句。

英文:

you can write

import(
  functions &quot;./functions&quot; 
)
func main(){
  c:= functions.getValue() &lt;-
}

If you write in gopath write this import functions &quot;MyProj/functions&quot; or if you are working with Docker

答案5

得分: 4

在Go包中,如果标识符名称的第一个字母以大写字母开头,那么所有标识符都将被导出到其他包中。

=> 将getValue()更改为GetValue()

英文:

In Go packages, all identifiers will be exported to other packages if the first letter of the identifier name starts with an uppercase letter.

=> change getValue() to GetValue()

答案6

得分: 4

你需要在项目的根目录中创建一个 go.mod 文件:go mod init module_name

  • 暴露的函数名应以大写字母开头
    import(
       "module_name/functions" 
    )
    func main(){
      functions.SomeFunction()
    }
英文:
  • you need to create a go.mod file in the root directory of your project: go mod init module_name
  • the name of exposed function should start with capital letter
    import(
       &quot;module_name/functions&quot; 
    )
    func main(){
      functions.SomeFunction()
    }

huangapple
  • 本文由 发表于 2014年10月1日 21:04:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/26142074.html
匿名

发表评论

匿名网友

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

确定