第一次用go不清楚如何调用这个类 希望各位帮帮忙

hellsam · · 2820 次点击
要看你是怎么用的SinAPIClient,报的什么错 从上面的代码来看,关键的就是下面3个field c := &SinAPIClient{ Username: "xxx", Password: "xxx", RPC: xxx, } c.Login() c.APICall()
#5
更多评论
```go package client import ( "errors" "fmt" log "github.com/Sirupsen/logrus" "github.com/nilshell/xmlrpc" ) type SinAPIClient struct { Session interface{} Host string Url string Username string Password string RPC *xmlrpc.Client } type APIResult struct { Status string Value interface{} ErrorDescription string } type XenAPIObject struct { Ref string Client *SinAPIClient } func (c *SinAPIClient) RPCCall(result interface{}, method string, params []interface{}) (err error) { log.Debugf("RPCCall method=%v params=%v\n", method, params) p := new(xmlrpc.Params) p.Params = params err = c.RPC.Call(method, *p, result) return err } func (client *SinAPIClient) Login() (err error) { //Do loging call result := xmlrpc.Struct{} params := make([]interface{}, 2) params[0] = client.Username params[1] = client.Password err = client.RPCCall(&result, "session.login", params) if err == nil { // err might not be set properly, so check the reference if result["Value"] == nil { return errors.New("Invalid credentials supplied") } } client.Session = result["Value"] return err } func (client *SinAPIClient) APICall(result *APIResult, method string, params ...interface{}) (err error) { if client.Session == nil { log.Errorf("no session\n") return fmt.Errorf("No session. Unable to make call") } //Make a params slice which will include the session p := make([]interface{}, len(params)+1) p[0] = client.Session if params != nil { for idx, element := range params { p[idx+1] = element } } res := xmlrpc.Struct{} err = client.RPCCall(&res, method, p) if err != nil { return err } result.Status = res["Status"].(string) if result.Status != "Success" { log.Errorf("Encountered an API error: %v %v", result.Status, res["ErrorDescription"]) return fmt.Errorf("API Error: %s", res["ErrorDescription"]) } else { result.Value = res["Value"] } return } ```
#1
![请把jpg改txt.jpg](https://static.studygolang.com/170902/ff4f9563ec462441527562babdf74391.jpg) 额 发布的代码都乱的 附件发不上来 请下载这个jpg文件 后缀改txt 希望大牛帮帮忙 怎么弄得呢
#2