英文:
How to call Go function of test1 in test2
问题
以下是翻译好的内容:
在下面的代码中,有一个名为goClientLib的包,其中包含一个名为readInput的函数。这个函数会读取命令行输入,并返回3个字符串。
package goClientLib
import (
....
)
//以下函数将读取命令行输入,并返回3个字符串
func readInput() (string, string, string){
var clientRequest, clientId, clientPassword string
argsLen := len(os.Args)
fmt.Println("参数长度:", argsLen)
if len(os.Args) != 4 {
fmt.Fprintf(os.Stderr, "用法: %s URL\n", os.Args[0])
os.Exit(1)
} else {
clientRequest = strings.Join(os.Args[1:2], "")
clientId = strings.Join(os.Args[2:3], "")
clientPassword = strings.Join(os.Args[3:4], "")
}
return clientRequest, clientId, clientPassword
}
现在我正在尝试在Test2.go文件中使用它,如下所示:
package main
import (
"os"
"fmt"
"net/http"
"io"
"log"
"goClientLib"
)
func main() {
clientRequest, clientId, clientPassword := goClientLib.readInput()
host := goClientLib.generateRequest(clientRequest)
fmt.Println("clientRequest:", clientRequest)
fmt.Println("clientId:", clientId)
fmt.Println("clientPassword:", clientPassword)
fmt.Println("host:", host)
response, err := http.Get(host)
if err != nil {
log.Fatal(err)
} else {
defer response.Body.Close()
_, err := io.Copy(os.Stdout, response.Body)
if err != nil {
log.Fatal(err)
}
}
}
我使用以下文件结构:
src/test2.go
src/goClientLib/test1.go
但是,当我运行这段代码时,出现以下错误:
# command-line-arguments
src\goClientMain.go:15: 无法引用未导出的名称goClientLib.readInput
src\goClientMain.go:16: 无法引用未导出的名称goClientLib.generateRequest
src\goClientMain.go:16: 未定义:goClientLib.generateRequest
英文:
go file in as below
package goClientLib
import (
....
)
//The following function will read Command Line Inputs and will return 3 strings
func readInput() (string, string, string){
var (clientRequest, clientId, clientPassword string)
argsLen := len(os.Args)
fmt.Println("Arg Length:",argsLen)
if len(os.Args) != 4 {
fmt.Fprintf(os.Stderr, "Usage: %s URL\n", os.Args[0])
os.Exit(1)
} else {
clientRequest = strings.Join(os.Args[1:2],"")
clientId = strings.Join(os.Args[2:3],"")
clientPassword = strings.Join(os.Args[3:4],"")
}
return clientRequest, clientId, clientPassword
}
Now I am trying to use it in Test2.go file as shown below:
package main
import (
"os"
"fmt"
"net/http"
"io"
"log"
"goClientLib"
)
func main() {
clientRequest, clientId, clientPassword := goClientLib.readInput()
host := goClientLib.generateRequest(clientRequest)
fmt.Println("clientRequest:",clientRequest)
fmt.Println("clientId:",clientId)
fmt.Println("clientPassword:",clientPassword)
fmt.Println("host:",host)
response, err := http.Get(host)
if err != nil {
log.Fatal(err)
} else {
defer response.Body.Close()
_, err := io.Copy(os.Stdout, response.Body)
if err != nil {
log.Fatal(err)
}
}
}
I am using Following File Structure
src/test2.go
src/goClientLib/test1.go
But This code Give me following error while running
# command-line-arguments
src\goClientMain.go:15: cannot refer to unexported name goClientLib.readInput
src\goClientMain.go:16: cannot refer to unexported name goClientLib.generateRequest
src\goClientMain.go:16: undefined: goClientLib.generateRequest
答案1
得分: 3
如Volker所评论的,为了访问另一个包中的函数,函数名的首字母必须大写。在你的情况下,将readInput()
改为ReadInput()
,将generateRequest()
改为GenerateRequest()
,并确保GenerateRequest()
函数在goClientLib
包中定义。点击这里获取更多信息。
英文:
As Volker commented, in order to access functions from another package, 1st letter of function name must be capital. In your case, change readInput()
to ReadInput()
and generateRequest()
to GenerateRequest()
and make sure GenerateRequest()
function is defined in goClientLib
package. Chack this for more information.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论