Go + Azure:调用一个方法返回 undefined

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

Go + Azure: Calling a method return undefined

问题

我正在尝试使用Go Azure SDK调用通知中心API。

我已经安装了SDK并导入到GO文件中:

package hub

import (
	"fmt"
	"github.com/Azure/azure-sdk-for-go/arm/notificationhubs"
)

func GetHub() {
	if resourceType, err := notificationhubs.Get("sourceGroupName", "NameSpaceValue", "NameOfTheHub"); err != nil {
		fmt.Println("Error occurred")
		return
	}
	fmt.Println("Success")
}

然而,当我尝试运行这段代码时,我得到了以下错误:

undefined: notificationhubs.Get

我不确定这是什么意思,因为我的IDE没有关于导入Azure SDK的投诉,所以我认为SDK已经正确导入了。

英文:

Am trying to use Go Azure SDK to call the notification hub api

I have installed the SDK and imported to the GO file :

package hub

import (

	"fmt"

	"github.com/Azure/azure-sdk-for-go/arm/notificationhubs"
)

func GetHub() {
	if resourceType, err := notificationhubs.Get("sourceGroupName", "NameSpaceValue", "NameOfTheHub"); err != nil {
		fmt.Println("Error occured")
		return
	}
	fmt.Println("Success")

}

However when am trying to runt he code i got this error

undefined: notificationhubs.Get

And am not sure what it means since my IDE don't complain about importing the Azure SDK so am assuming the SDK is imported correctly.

答案1

得分: 1

你正在尝试使用的函数不存在(https://godoc.org/github.com/Azure/azure-sdk-for-go/arm/notificationhubs)。

你可能正在尝试使用GroupClient.Get函数;如果是这样的话,你需要获取一个类型为GroupClient的对象,然后在该对象上调用Get函数。

英文:

The function you're trying to use doesn't exist (https://godoc.org/github.com/Azure/azure-sdk-for-go/arm/notificationhubs).

You're probably trying to use the function GroupClient.Get; if that's the case, you need to get an object of type GroupClient and then call the function Get on it.

答案2

得分: 0

@cd1是正确的!Get方法不直接属于你导入的命名空间,而是属于该命名空间中存在的一个客户端。为了以这种方式与NotificationsHub进行交互,需要实例化一个GroupClient,然后运行Get命令。

import (
    hubs "github.com/Azure/azure-sdk-for-go/arm/notificationshub"
)

func main() {
    // 你程序的实现细节...
    client := hubs.NewGroupClient("<YOUR SUBSCRIPTION ID>")
    client.Authorizer = // 你初始化的服务主体令牌

    results, err := client.Get("<RESOURCE GROUP NAME>", "<NAMESPACE NAME>", "<NOTIFICATION HUB NAME>")
    if err != nil {
        return
    }
}
英文:

@cd1 is correct! The Get method doesn't belong directly to namespace that you've imported, but rather a client that exists in that namespace. In order to interact with NotificationsHub in this manner, instantiate a GroupClient then run a Get command.

import (
    hubs &quot;github.com/Azure/azure-sdk-for-go/arm/notificationshub&quot;
)

func main() {
    // Implementation details of your program ...
    client := hubs.NewGroupClient(&quot;&lt;YOUR SUBSCRIPTION ID&gt;&quot;)
    client.Authorizer = // Your initialized Service Principal Token

    results, err := client.Get(&quot;&lt;RESOURCE GROUP NAME&gt;&quot;, &quot;&lt;NAMESPACE NAME&gt;&quot;, &quot;&lt;NOTIFICATION HUB NAME&gt;&quot;)
    if err != nil {
        return
    }
}

huangapple
  • 本文由 发表于 2017年6月5日 20:21:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/44368841.html
匿名

发表评论

匿名网友

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

确定