我有两个文件 main.go
位于 package main
下,另一个文件包含包中的一些函数,称为 functions。
我的问题是:如何从 package main
调用函数?
文件 1:main.go(位于 MyProj/main.go)
package main
import "fmt"
import "functions" // I dont have problem creating the reference here
func main(){
c:= functions.getValue() // <---- this is I want to do
}
文件 2:functions.go(位于 MyProj/functions/functions.go)
package functions
func getValue() string{
return "Hello from this another package"
}
最佳答案
您通过包的导入路径导入包,并通过包名引用其所有导出的符号(那些以以大写字母开头的符号),如下所示:
import "MyProj/functions"
functions.GetValue()
关于go - 从 Go 中的另一个包调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26142074/