2
去文件test1的围棋功能如下如何调用test2的
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
}
现在我想在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
但是这个代码能给我一边跑
以下错误0# 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
编译器错误非常明显:您完全无法访问其他软件包中未导出的内容。出口。并阅读https://golang.org/doc/code.html。 – Volker
@Volker不完全正确。你可以使用反射包来访问一些未导出的东西(例如未导出的结构域)。尽管如此,这通常是个坏主意。我不认为有任何方法可以访问未导出的函数。 – jussius