http:panic serving [:: 1]:57685:分配给nil map中的条目

时间:2018-01-21 20:09:21

标签: json api go vcard vcf

我正在尝试使用golang制作一个vcard。我的代码是:

    var (
    // card is a map of strings to []*vcard.Field objects
    card vcard.Card

    // destination where the vcard will be encoded to
    enc = vcard.NewEncoder(destFile) 
)
var str []string


for i, entry := range k {


    if i!=2{
        str=append(str,k[i])


        card.SetValue(vcard.FieldFormattedName, strings.Join(str[:i],""))//panic occurs here
        fmt.Println(str)

    }else if i==2{
        card.SetValue(vcard.FieldTelephone, entry)
    }else{
        card.SetValue(vcard.FieldOrganization, entry)
    }




    // set the value of a field and other parameters by using card.Set
    card.Set(vcard.FieldName, &vcard.Field{
        Value: strings.Join(str[:2], ";"),
        Params: map[string][]string{
            vcard.ParamSortAs: []string{
                k[0] + " " + k[1],
            },
        },
    })

必须存储在vcf中的json文档是:

      {"firstname":"Vilas","lastname":"Prakash","mobile":"8732647482","organisation":"Dbjb"}

我已将json编组为[] string.On执行时出现以下错误:

    http: panic serving [::1]:57685: assignment to entry in nil map

任何人都可以帮助我???或者有没有其他方法在golang中使用json文件作为输入创建.vcf或vcard ???

1 个答案:

答案 0 :(得分:1)

您尚未初始化地图。我建议您阅读Go https://blog.golang.org/go-maps-in-action

中地图的工作原理

与此同时

var card vcard.Card

应该是

var card = make(vcard.Card)

我不熟悉vcard数据结构。但是你提到它应该是一张地图 - > string] [] * vcard.Field ..如果上面的make失败,请将其更改为

var card = make(map[string][]*vcard.Field)