在Go中调用另一个包中的函数 [英] Call a function from another package in Go

查看:5315
本文介绍了在Go中调用另一个包中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有两个文件 main.go ,它位于 package main ,以及另一个包含函数的函数的文件。



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



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

  package main 

importfmt
importfunctions //我没有在这里创建引用的问题

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





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

 包函数

func getValue()string {
返回你从这个另一个包
}

非常感谢您的帮助。 / p>

解决方案

您通过其导入包导入路径,并通过包名称引用其所有导出的符号(以开头的大写字母),如下所示:

  importMyProj / functions

functions.GetValue()


Hi Im newbie in golang.

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"
}

Thanks so much for your help.

解决方案

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()

这篇关于在Go中调用另一个包中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆