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

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

Go + Azure: Calling a method return undefined

问题

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

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

  1. package hub
  2. import (
  3. "fmt"
  4. "github.com/Azure/azure-sdk-for-go/arm/notificationhubs"
  5. )
  6. func GetHub() {
  7. if resourceType, err := notificationhubs.Get("sourceGroupName", "NameSpaceValue", "NameOfTheHub"); err != nil {
  8. fmt.Println("Error occurred")
  9. return
  10. }
  11. fmt.Println("Success")
  12. }

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

  1. 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 :

  1. package hub
  2. import (
  3. "fmt"
  4. "github.com/Azure/azure-sdk-for-go/arm/notificationhubs"
  5. )
  6. func GetHub() {
  7. if resourceType, err := notificationhubs.Get("sourceGroupName", "NameSpaceValue", "NameOfTheHub"); err != nil {
  8. fmt.Println("Error occured")
  9. return
  10. }
  11. fmt.Println("Success")
  12. }

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

  1. 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命令。

  1. import (
  2. hubs "github.com/Azure/azure-sdk-for-go/arm/notificationshub"
  3. )
  4. func main() {
  5. // 你程序的实现细节...
  6. client := hubs.NewGroupClient("<YOUR SUBSCRIPTION ID>")
  7. client.Authorizer = // 你初始化的服务主体令牌
  8. results, err := client.Get("<RESOURCE GROUP NAME>", "<NAMESPACE NAME>", "<NOTIFICATION HUB NAME>")
  9. if err != nil {
  10. return
  11. }
  12. }
英文:

@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.

  1. import (
  2. hubs &quot;github.com/Azure/azure-sdk-for-go/arm/notificationshub&quot;
  3. )
  4. func main() {
  5. // Implementation details of your program ...
  6. client := hubs.NewGroupClient(&quot;&lt;YOUR SUBSCRIPTION ID&gt;&quot;)
  7. client.Authorizer = // Your initialized Service Principal Token
  8. results, err := client.Get(&quot;&lt;RESOURCE GROUP NAME&gt;&quot;, &quot;&lt;NAMESPACE NAME&gt;&quot;, &quot;&lt;NOTIFICATION HUB NAME&gt;&quot;)
  9. if err != nil {
  10. return
  11. }
  12. }

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:

确定