package main import ( "fmt" "golang.org/x/text/encoding" "golang.org/x/text/encoding/charmap" ) type Encoding string const ( EncodingUtf8 Encoding = "UTF-8" EncodingWin1251 Encoding = "Win-1251" ) func (e Encoding) Encoder() (*encoding.Encoder, error) { switch e { case EncodingWin1251: return charmap.Windows1251.NewEncoder(), nil case EncodingUtf8: return &encoding.Encoder{Transformer: new(DummyTransformer)}, nil } return nil, fmt.Errorf("unknown encoding: %s", string(e)) } type DummyTransformer struct{} func (e *DummyTransformer) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { copy(dst, src) return len(dst), len(src), nil } func (e *DummyTransformer) Reset() {}