I am trying to make a vcard using golang.My code is:
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],
},
},
})
The json document which has to be stored in vcf is:
{"firstname":"Vilas","lastname":"Prakash","mobile":"8732647482","organisation":"Dbjb"}
I have marshalled the json to []string.On executing I am getting the following error:
http: panic serving [::1]:57685: assignment to entry in nil map
Can anyone help me???Or Is there any other way of creating .vcf or vcard in golang using json document as input???
You haven't initialized your map. I suggest you read up on how maps work in Go https://blog.golang.org/go-maps-in-action
In the meantime
var card vcard.Card
should be
var card = make(vcard.Card)
I'm not familiar with the vcard data structure. But you mention that it should be a map -> string][]*vcard.Field.. If the above make fails change it to
var card = make(map[string][]*vcard.Field)