如何在test2中调用test1的Go函数?

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

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.

huangapple
  • 本文由 发表于 2016年12月1日 17:32:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/40906921.html
匿名

发表评论

匿名网友

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

确定