英文:
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 "fmt"
import "functions" // I dont have problem creating the reference here
func main(){
c:= functions.getValue() // <---- this is I want to do
}
File 2: functions.go (located in MyProj/functions/functions.go)
package functions
func getValue() string{
return "Hello from this another package"
}
答案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 "MyProj/functions"
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 itmain
or not. It will be named asMyProj
. -
package main
just denotes that this file has an executable command which containsfunc main()
. Then, you can run this code as:go run main.go
. See here for more info. -
You should rename your
func getValue()
infunctions
package tofunc 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 (
"fmt"
"MyProj/functions"
)
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 "Hello from this another package"
}
答案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 "./functions"
)
func main(){
c:= functions.getValue() <-
}
If you write in gopath
write this import functions "MyProj/functions"
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(
"module_name/functions"
)
func main(){
functions.SomeFunction()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论