如何在golang中获取带引号的可打印字符串

时间:2017-11-15 04:30:10

标签: go

如何获取根据»RFC2045,第6.7节创建的带引号的可打印字符串。在golang

PHP等价物是“quoted_printable_encode(string $ str)”

1 个答案:

答案 0 :(得分:1)

使用standard quotedprintable package对字符串进行编码:

...

import (
    "bytes"
    "mime/quotedprintable"
    "github.com/pkg/errors"
)

...

func toQuotedPrintable(s string) (string, error) {
    var ac bytes.Buffer
    w := quotedprintable.NewWriter(&ac)
    _, err := w.Write([]byte(s))
    if err != nil {
        return "", errors.Wrap(err, "write")
    }
    err = w.Close()
    if err != nil {
        return "", errors.Wrap(err, "close")
    }
    return ac.String(), nil
}