emersion / go-vcard

A Go library to parse and format vCard
MIT License
107 stars 34 forks source link

example error #25

Closed Phuong39 closed 2 years ago

Phuong39 commented 2 years ago

example error:


func main() {
    destFile, err := os.Create(".\Contacts2.vcf")
    if err != nil {
        log.Fatal(err)
    }
    defer destFile.Close()

    // data in order: first name, middle name, last name, telephone number
    contacts := [][4]string{
        {"John", "Webber", "Maxwell", "(+1) 199 8714"},
        {"Donald", "", "Ron", "(+44) 421 8913"},
        {"Eric", "E.", "Peter", "(+37) 221 9903"},
        {"Nelson", "D.", "Patrick", "(+1) 122 8810"},
    }

    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)
    )

    for _, entry := range contacts {
        // set only the value of a field by using card.SetValue.
        // This does not set parameters
        card.SetValue(vcard.FieldFormattedName, strings.Join(entry[:3], " "))
        card.SetValue(vcard.FieldTelephone, entry[3])

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

        // make the vCard version 4 compliant
        vcard.ToV4(card)
        err := enc.Encode(card)
        if err != nil {
            log.Fatal(err)
        }
    }
}

panic: assignment to entry in nil map

goroutine 1 [running]:
github.com/emersion/go-vcard.Card.Set(...)
D:/gopath/src/github.com/emersion/go-vcard/card.go:118
github.com/emersion/go-vcard.Card.SetValue(...)
D:/gopath/src/github.com/emersion/go-vcard/card.go:166
main.main()
/vcard_cretor2.go:110 +0x3cd

emersion commented 2 years ago

You cannot write to a nil vcard.Card. Yu need to make() it before use.