emersion / go-vcard

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

How can I convert enc to string #24

Closed hasanAjsf closed 2 years ago

hasanAjsf commented 2 years ago

I wrote the below that created vcard, but once I tried to read the vcard back to its string I failed:

package main

import (
    "io"
    "log"
    "strings"

    "github.com/emersion/go-vcard"
)

func main() {

    var content strings.Builder
    content.WriteString("BEGIN:VCARD\n")
    content.WriteString("VERSION:3.0\n")
    content.WriteString("FN:Hasan Yousef\n")
    content.WriteString("TEL:999\n")
    content.WriteString("Fitem1.TEL;waid=966000000000\n")
    content.WriteString("item1.X-ABLabel:Mobile\n")
    content.WriteString("END:VCARD")

    r := strings.NewReader(content.String())
    dec := vcard.NewDecoder(r)
    var card vcard.Card
    var err error
    for {
        card, err = dec.Decode()
        if err == io.EOF {
            break
        } else if err != nil {
            log.Fatal(err)
        }

        log.Println(card.PreferredValue(vcard.FieldFormattedName))
        log.Println(card.PreferredValue(vcard.FieldTelephone))
    }

    var enc vcard.Encoder

    // enc = vcard.NewEncoder(destFile)
    err = enc.Encode(card)
    if err != nil {
        log.Fatal(err)
    }

    // How can I convert enc to string!

    log.Println(&enc)

}

I got:

2022/05/07 12:44:47 Hasan Yousef
2022/05/07 12:44:47 999
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x18 pc=0x10549d]

goroutine 1 [running]:
io.WriteString({0x0, 0x0}, {0x15c770, 0xd})
        D:/Development/go/src/io/io.go:314 +0x7d
github.com/emersion/go-vcard.(*Encoder).Encode(0xc00004a360, 0x3?)
        D:/Development/gopath/pkg/mod/github.com/emersion/go-vcard@v0.0.0-20210521075357-3445b9171995/encoder.go:23 +0x57
main.main()
        D:/Desktop/main.go:41 +0x77e
exit status 2
emersion commented 2 years ago

GitHub issues aren't for questions, sorry.