AWS Lambda上golang的解组错误,在本地工作

时间:2019-03-24 18:44:52

标签: amazon-web-services go aws-lambda

我在golang中创建了应该支持端点API(通过get查询)的代码。那是API端点的文档: https://developer.dotdigital.com/docs/get-all-campaigns

代码如下:

type Campaign struct {
    Id          int    `json:"id,omitempty"`
    Name        string `json:"name,omitempty"`
    Subject     string `json:"subject,omitempty"`
    FromName    string `json:"fromName,omitempty"`
    FromAddress struct {
        Id    int    `json:"id,omitempty"`
        Email string `json:"email,omitempty"`
    }
    HtmlContent      string `json:"htmlContent,omitempty"`
    PlainTextContent string `json:"plainTextContent,omitempty"`
    ReplyAction      string `json:"replyAction,omitempty"`
    IsSplitTest      bool   `json:"isSplitTest,omitempty"`
    Status           string `json:"status,omitempty"`
}

func (dcfg DotmailerApiConfig) GetContacts2() ([]*dotmailermodels.Contact) {
    var (
        allContacts, respContacts []*dotmailermodels.Contact
        selected                  = 1000
        skip                      = 0
        err error
    )
    for true {
        url := dcfg.Url + fmt.Sprintf("v2/contacts?withFullData=%s&select=%s&skip=%s",
            strconv.FormatBool(false),
            strconv.Itoa(selected),
            strconv.Itoa(skip))
        resp := dcfg.GetRequesDotmailertBuilder(url)
        err = json.Unmarshal(resp, &respContacts)
        if err != nil {
            Error.Println(err) // just error trace
        }
        allContacts = append(allContacts, respContacts...)
        if len(respContacts) == 1000 {
            skip += 1000
            respContacts = nil
            continue
        }
        break
    }
    return allContacts
}


在PC上运行时,我会得到正确的响应。在Lambda中使用它时,出现此错误:

[ERROR] 2019/03/24 18:37:26 dotmailergetrequests.go:110: json: cannot unmarshal object into Go value of type []*dotmailermodels.Campaign

你知道为什么吗?

2 个答案:

答案 0 :(得分:1)

尝试一下:

type Address struct {
    Id    int    `json:"id,omitempty"`
    Email string `json:"email,omitempty"`
}
type Campaign struct {
    Id          int    `json:"id,omitempty"`
    Name        string `json:"name,omitempty"`
    Subject     string `json:"subject,omitempty"`
    FromName    string `json:"fromName,omitempty"`
    FromAddress *Address `json:"fromAddress,omitempty"`
    HtmlContent      string `json:"htmlContent,omitempty"`
    PlainTextContent string `json:"plainTextContent,omitempty"`
    ReplyAction      string `json:"replyAction,omitempty"`
    IsSplitTest      bool   `json:"isSplitTest,omitempty"`
    Status           string `json:"status,omitempty"`
}

答案 1 :(得分:0)

我发现了这个错误。一切都正确-除了在其他文件中导入密码外。