为什么Go包显示为未定义?

时间:2017-09-19 01:12:01

标签: rest go packages

我无法理解下面代码的问题。 Visual Studio Code编译器给出了以下错误,因为包" util"未定义。

'Error' message: 'undefined: util.GetPortAndURL

我将以下内容放在util.go文件的顶部

package util

我还在我的handlers.go文件中导入了包myproj / util,该文件是在其init方法中调用以下代码的文件。

file, err := ioutil.ReadFile("./creds.json")
if err != nil {
    fmt.Printf("File error: %v\n", err)
    // use a better error pattern here
}
json.Unmarshal(file, &oathCred)
port, url := util.GetPortAndURL() <-------- Here
if (port != nil) && (url != nil) {
    oathConf = &oauth2.Config{
        ClientID:     oathCred.ClientID,
        ClientSecret: oathCred.ClientSecret,
        RedirectURL:  url + ":" + string(port) + "/auth",
        Scopes: []string{
            "https://www.googleapis.com/auth/userinfo.email", // You have to select your own scope from here -> https://developers.google.com/identity/protocols/googlescopes#google_sign-in
        },
        Endpoint: google.Endpoint,
    }
}

getPortAndURL()函数位于util.go中,如下所示

// GetPortandURL returns the URL and Port currently used
/*
'
This function should be used to set the port and url for the program until a
set function is created.
'
*/
func GetPortandURL() (int, string) {
    port := 8080
    url := "http://127.0.0.1"
    return port, url
}

我的GOPATH是C:\ Users \ Me \ Documents \ code \ go

文件结构如下

go/src/
└── myproj
    ├── handlers
    ├── main
    └── util

1 个答案:

答案 0 :(得分:0)

将其称为port, url := GetPortAndURL()。我不相信它需要“util”。包含前缀,如果util.go(定义)在同一个包中。