Go GetLocations失败,返回“0”;对象不存在,无法在“上执行方法”;
我的电话有问题。每次尝试执行时,我都会收到错误: SoftLayer_异常:对象不存在,无法在其上执行方法。 (软层位置组::getLocations)(HTTP 200) 这让我觉得我创建的locationService对象有问题,但我不明白是什么。有人看到这个问题吗Go GetLocations失败,返回“0”;对象不存在,无法在“上执行方法”;,go,ibm-cloud-infrastructure,Go,Ibm Cloud Infrastructure,我的电话有问题。每次尝试执行时,我都会收到错误: SoftLayer_异常:对象不存在,无法在其上执行方法。 (软层位置组::getLocations)(HTTP 200) 这让我觉得我创建的locationService对象有问题,但我不明白是什么。有人看到这个问题吗 package main import ( "fmt" "github.com/softlayer/softlayer-go/session" "github.com/softlayer/softlay
package main
import (
"fmt"
"github.com/softlayer/softlayer-go/session"
"github.com/softlayer/softlayer-go/services"
)
func main() {
sess := session.New("user", "password")
locationService := services.GetLocationGroupService(sess)
locations, err := locationService.GetLocations()
if err != nil {
fmt.Printf("%s\n",err.Error())
return
}
fmt.Printf("%+v", locations)
}
出现的错误是因为需要使用标识符locationGroup ID 将此locationGroupId添加到you go代码中,如以下示例所示:
locationGroupId := 1
// Create a session
sess := session.New(username, apikey)
// Get SoftLayer_Location_Group
service := services.GetLocationGroupService(sess)
result, err := service.Id(locationGroupId).GetLocations()
参考:
要获得所有可用的LocationGroupID,可以使用以下go代码示例:
package main
/*
GetAllObjects
Retrieve all locationGroup objects.
Important manual pages:
https://softlayer.github.io/reference/services/SoftLayer_Location_Group/
https://softlayer.github.io/reference/services/SoftLayer_Location_Group/getAllObjects/
License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
*/
import (
"fmt"
"github.com/softlayer/softlayer-go/services"
"github.com/softlayer/softlayer-go/session"
"encoding/json"
)
func main() {
// SoftLayer API username and key
username := "set me"
apikey := "set me"
// Create a session
sess := session.New(username, apikey)
// Get SoftLayer_Account service
service := services.GetLocationGroupService(sess)
result, err := service.GetAllObjects()
if err != nil {
fmt.Printf("\n Unable to retrieve all locationGroups:\n - %s\n", err)
return
}
// Following helps to print the result in json format.
jsonFormat, jsonErr := json.MarshalIndent(result,""," ")
if jsonErr != nil {
fmt.Println(jsonErr)
return
}
fmt.Println(string(jsonFormat))
}
主程序包
/*
GetAllObject
检索所有locationGroup对象。
重要手册页:
https://softlayer.github.io/reference/services/SoftLayer_Location_Group/
https://softlayer.github.io/reference/services/SoftLayer_Location_Group/getAllObjects/
许可证:http://sldn.softlayer.com/article/License
作者:SoftLayer Technologies,Inc。
*/
进口(
“fmt”
“github.com/softlayer/softlayer go/services”
“github.com/softlayer/softlayer go/session”
“编码/json”
)
func main(){
//SoftLayer API用户名和密钥
用户名:=“设置我”
apikey:=“设置我”
//创建会话
sess:=session.New(用户名,apikey)
//获取SoftLayer\u帐户服务
服务:=服务。GetLocationGroupService(sess)
结果,错误:=service.GetAllObjects()
如果错误!=零{
fmt.Printf(“\n无法检索所有位置组:\n-%s\n”,错误)
返回
}
//以下内容有助于以json格式打印结果。
jsonFormat,jsonErr:=json.marshallindent(结果为“”,“”)
如果jsoner!=nil{
fmt.Println(JSONER)
返回
}
fmt.Println(字符串(jsonFormat))
}