本文整理汇总了Golang中mime.WordDecoder.CharsetReader方法的典型用法代码示例。如果您正苦于以下问题:Golang WordDecoder.CharsetReader方法的具体用法?Golang WordDecoder.CharsetReader怎么用?Golang WordDecoder.CharsetReader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mime.WordDecoder
的用法示例。
在下文中一共展示了WordDecoder.CharsetReader方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ExampleWordDecoder_Decode
func ExampleWordDecoder_Decode() {
dec := new(mime.WordDecoder)
header, err := dec.Decode("=?utf-8?q?=C2=A1Hola,_se=C3=B1or!?=")
if err != nil {
panic(err)
}
fmt.Println(header)
dec.CharsetReader = func(charset string, input io.Reader) (io.Reader, error) {
switch charset {
case "x-case":
// Fake character set for example.
// Real use would integrate with packages such
// as code.google.com/p/go-charset
content, err := ioutil.ReadAll(input)
if err != nil {
return nil, err
}
return bytes.NewReader(bytes.ToUpper(content)), nil
default:
return nil, fmt.Errorf("unhandled charset %q", charset)
}
}
header, err = dec.Decode("=?x-case?q?hello!?=")
if err != nil {
panic(err)
}
fmt.Println(header)
// Output:
// ¡Hola, señor!
// HELLO!
}
示例2: DecodeHeader
// DecodeHeader (per RFC 2047) using Golang's mime.WordDecoder
func DecodeHeader(input string) string {
if !strings.Contains(input, "=?") {
// Don't scan if there is nothing to do here
return input
}
dec := new(mime.WordDecoder)
dec.CharsetReader = NewCharsetReader
header, err := dec.DecodeHeader(input)
if err != nil {
return input
}
return header
}
示例3: NonASCII
// NonASCII Decode non ASCII header string RFC 1342
func NonASCII(encoded string) string {
regexRFC1342, _ := regexp.Compile(`=\?.*?\?=`)
dec := new(mime.WordDecoder)
dec.CharsetReader = charset.NewReader
result := regexRFC1342.ReplaceAllStringFunc(encoded, func(encoded string) string {
decoded, err := dec.Decode(encoded)
if err != nil {
log.Println("Error decode NonASCII", encoded, err)
return encoded
}
return decoded
})
return result
}
示例4: WordDecoder
func WordDecoder() *mime.WordDecoder {
decoder := new(mime.WordDecoder)
decoder.CharsetReader = CharsetReader
return decoder
}