教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 Golang标准库——encoding(1)

Golang标准库——encoding(1)

发布时间:2022-02-15   编辑:jiaochengji.com
教程集为您提供Golang标准库——encoding(1)等资源,欢迎您收藏本站,我们将为您提供最新的Golang标准库——encoding(1)资源
<ul><li>encoding</li> <li>ascii85</li> <li>asn1</li> <li>base32</li> <li>base64</li> </ul><h2>encoding</h2> <blockquote>

encoding包定义了供其它包使用的可以将数据在字节水平和文本表示之间转换的接口。encoding/gob、encoding/json、encoding/xml三个包都会检查使用这些接口。因此,只要实现了这些接口一次,就可以在多个包里使用。标准包内建类型time.Time和net.IP都实现了这些接口。接口是成对的,分别产生和还原编码后的数据。

</blockquote> <h3>type BinaryMarshaler</h3> <pre><code class="lang-go hljs">type BinaryMarshaler interface { MarshalBinary() (data []byte, err error) } </code></code></pre> <blockquote>

实现了BinaryMarshaler接口的类型可以将自身序列化为binary格式。

</blockquote> <h3>type BinaryUnmarshaler</h3> <pre><code class="lang-go hljs">type BinaryUnmarshaler interface { UnmarshalBinary(data []byte) error } </code></code></pre> <blockquote>

实现了BinaryUnmarshaler接口的类型可以将binary格式表示的自身解序列化。

UnmarshalBinary必须可以解码MarshalBinary生成的binary格式数据。本函数可能会对data内容作出修改,所以如果要保持data的数据请事先进行拷贝。

</blockquote> <h3>type TextMarshaler</h3> <pre><code class="lang-go hljs">type TextMarshaler interface { MarshalText() (text []byte, err error) } </code></code></pre>

实现了BinaryMarshaler接口的类型可以将自身序列化为utf-8编码的textual格式。

<h3>type TextUnmarshaler</h3> <pre><code class="lang-go hljs">type TextUnmarshaler interface { UnmarshalText(text []byte) error } </code></code></pre> <blockquote>

实现了TextUnmarshaler接口的类型可以将textual格式表示的自身解序列化。

UnmarshalText必须可以解码MarshalText生成的textual格式数据。本函数可能会对data内容作出修改,所以如果要保持data的数据请事先进行拷贝。

</blockquote> <h2>ascii85</h2> <blockquote>

ascii85包实现了ascii85数据编码(5个ascii字符表示4个字节),该编码用于btoa工具和Adobe的PostScript语言和PDF文档格式。

</blockquote> <h3>type CorruptInputError</h3> <pre><code class="lang-go hljs">type CorruptInputError int64 </code></code></pre> <h4>func (CorruptInputError) Error</h4> <pre><code class="lang-go hljs">func (e CorruptInputError) Error() string </code></code></pre> <h3>func MaxEncodedLen</h3> <pre><code class="lang-go hljs">func MaxEncodedLen(n int) int </code></code></pre> <blockquote>

返回n字节源数据编码后的最大字节数。

</blockquote> <h3>func Encode</h3> <pre><code class="lang-go hljs">func Encode(dst, src []byte) int </code></code></pre> <blockquote>

将src编码成最多MaxEncodedLen(len(src))数据写入dst,返回实际写入的字节数。编码每4字节一段进行一次,最后一个片段采用特殊的处理方式,因此不应将本函数用于处理大数据流的某一独立数据块。

一般来说ascii85编码数据会被'<<sub>'和'</sub>>'包括起来,函数并未添加上它们。

</blockquote> <h3>func Decode</h3> <pre><code class="lang-go hljs">func Decode(dst, src []byte, flush bool) (ndst, nsrc int, err error) </code></code></pre> <blockquote>

将src解码后写入dst,返回写入dst的字节数、从src解码的字节数。如果src含有非法数据,函数将返回成功执行的数据(两个数字)和CorruptInputError。如果flush为真,则函数会认为src代表输入流的结尾,完全处理src,而不会等待另一个32字节的数据块。

函数会忽略src中的空格和控制字符,一般来说ascii85编码数据会被'<<sub>'和'</sub>>'包括起来,但是调用者应自行去掉它们。

</blockquote> <h3>func NewEncoder</h3> <pre><code class="lang-go hljs">func NewEncoder(w io.Writer) io.WriteCloser </code></code></pre> <blockquote>

创建一个将数据编码为ascii85流写入w的编码器。Ascii85编码算法操作32位块,写入结束后,必须调用Close方法将缓存中保留的不完整块刷新到w里。

</blockquote> <h3>func NewDecoder</h3> <pre><code class="lang-go hljs">func NewDecoder(r io.Reader) io.Reader </code></code></pre> <blockquote>

创建一个从r解码ascii85流的解码器。

</blockquote> <h2>asn1</h2> <blockquote>

asn1包实现了DER编码的ASN.1数据结构的解析,参见ITU-T Rec X.690。

其他细节参见"A Layman's Guide to a Subset of ASN.1, BER, and DER"。

</blockquote> <h3>type SyntaxError</h3> <pre><code class="lang-go hljs">type SyntaxError struct { Msg string } </code></code></pre> <blockquote>

SyntaxErrorLeixing表示ASN.1数据不合法。

</blockquote> <h4>func (SyntaxError) Error</h4> <pre><code class="lang-go hljs">func (e SyntaxError) Error() string </code></code></pre> <h3>type StructuralError</h3> <pre><code class="lang-go hljs">type StructuralError struct { Msg string } </code></code></pre> <blockquote>

StructuralError表示ASN.1数据合法但接收的Go类型不匹配。

</blockquote> <h4>func (StructuralError) Error</h4> <pre><code class="lang-go hljs">func (e StructuralError) Error() string </code></code></pre> <h3>type RawContent</h3> <pre><code class="lang-go hljs">type RawContent []byte </code></code></pre> <blockquote>

RawContent用于标记未解码的应被结构体保留的DER数据。如要使用它,结构体的第一个字段必须是本类型,其它字段不能是本类型。

</blockquote> <h3>type RawValue</h3> <pre><code class="lang-go hljs">type RawValue struct { Class, Tag int IsCompound bool Bytes []byte FullBytes []byte // 包括标签和长度 } </code></code></pre> <blockquote>

RawValue代表一个未解码的ASN.1对象。

</blockquote> <h3>type Flag</h3> <pre><code class="lang-go hljs">type Flag bool </code></code></pre> <blockquote>

Flag接收任何数据,如果数据存在就设自身为真。

</blockquote> <h3>type Enumerated</h3> <pre><code class="lang-go hljs">type Enumerated int </code></code></pre> <blockquote>

Enumerated表示一个明文整数。

</blockquote> <h3>type BitString</h3> <pre><code class="lang-go hljs">type BitString struct { Bytes []byte // 字位流打包在字节流里 BitLength int // 字位流的长度 } </code></code></pre> <blockquote>

BitString类型是用于表示ASN.1 BIT STRING类型的结构体。字位流补齐到最近的字节数保存在内存里并记录合法字位数,补齐的位可以为0个。

</blockquote> <h4>func (BitString) At</h4> <pre><code class="lang-go hljs">func (b BitString) At(i int) int </code></code></pre> <blockquote>

At方法发挥index位置的字位,如果index出界则返回0。

</blockquote> <h4>func (BitString) RightAlign</h4> <pre><code class="lang-go hljs">func (b BitString) RightAlign() []byte </code></code></pre> <blockquote>

RightAlign方法返回b表示的字位流的右对齐版本(即补位在开始部分)切片,该切片可能和b共享底层内存。

</blockquote> <h3>type ObjectIdentifier</h3> <pre><code class="lang-go hljs">type ObjectIdentifier []int </code></code></pre> <blockquote>

ObjectIdentifier类型用于表示ASN.1 OBJECT IDENTIFIER类型。

</blockquote> <h4>func (ObjectIdentifier) Equal</h4> <pre><code class="lang-go hljs">func (oi ObjectIdentifier) Equal(other ObjectIdentifier) bool </code></code></pre> <blockquote>

如果oi和other代表同一个标识符,Equal方法返回真。

</blockquote> <h4>func (ObjectIdentifier) String</h4> <pre><code class="lang-go hljs">func (oi ObjectIdentifier) String() string </code></code></pre> <h3>func Marshal</h3> <pre><code class="lang-go hljs">func Marshal(val interface{}) ([]byte, error) </code></code></pre> <blockquote>

Marshal函数返回val的ASN.1编码。

此外还提供了供Unmarshal函数识别的结构体标签,可用如下标签:

</blockquote> <pre><code class="lang-go hljs">ia5: 使字符串序列化为ASN.1 IA5String类型 omitempty: 使空切片被跳过 printable: 使字符串序列化为ASN.1 PrintableString类型 utf8: 使字符串序列化为ASN.1 UTF8字符串 </code></code></pre> <h3>func Unmarshal</h3> <pre><code class="lang-go hljs">func Unmarshal(b []byte, val interface{}) (rest []byte, err error) </code></code></pre> <blockquote>

Unmarshal函数解析DER编码的ASN.1结构体数据并使用reflect包填写val指向的任意类型值。因为本函数使用了reflect包,结构体必须使用大写字母起始的字段名。

ASN.1 INTEGER 类型值可以写入int、int32、int64或*big.Int(math/big包)类型。类型不匹配会返回解析错误。

ASN.1 BIT STRING类型值可以写入BitString类型。

ASN.1 OCTET STRING类型值可以写入[]byte类型。

ASN.1 OBJECT IDENTIFIER类型值可以写入ObjectIdentifier类型。

ASN.1 ENUMERATED类型值可以写入Enumerated类型。

ASN.1 UTCTIME类型值或GENERALIZEDTIME 类型值可以写入time.Time类型。

ASN.1 PrintableString类型值或者IA5String类型值可以写入string类型。

以上任一ASN.1类型值都可写入interface{}类型。保存在接口里的类型为对应的Go类型,ASN.1整型对应int64。

如果类型x可以写入切片的成员类型,则类型x的ASN.1 SEQUENCE或SET类型可以写入该切片。

ASN.1 SEQUENCE或SET类型如果其每一个成员都可以写入某结构体的对应字段,则可以写入该结构体

对Unmarshal函数,下列字段标签有特殊含义:

</blockquote> <pre><code class="lang-go hljs">application 指明使用了APPLICATION标签 default:x 设置一个可选整数字段的默认值 explicit 给一个隐式的标签设置一个额外的显式标签 optional 标记字段为ASN.1 OPTIONAL的 set 表示期望一个SET而不是SEQUENCE类型 tag:x 指定ASN.1标签码,隐含ASN.1 CONTEXT SPECIFIC </code></code></pre> <blockquote>

如果结构体的第一个字段的类型为RawContent,则会将原始ASN1结构体内容包存在该字段。

如果切片成员的类型名以"SET"结尾,则视为该字段有"set"标签。这是给不能使用标签的嵌套切片使用的。

其它ASN.1类型不支持,如果遭遇这些类型,Unmarshal返回解析错误。

</blockquote> <h3>func UnmarshalWithParams</h3> <pre><code class="lang-go hljs">func UnmarshalWithParams(b []byte, val interface{}, params string) (rest []byte, err error) </code></code></pre> <blockquote>

UnmarshalWithParams允许指定val顶层成员的字段参数,格式和字段标签相同。

</blockquote> <h2>base32</h2> <blockquote>

base32包实现了RFC 4648规定的base32编码。

</blockquote> <h3>Variables</h3> <pre><code class="lang-go hljs">var HexEncoding = NewEncoding(encodeHex) </code></code></pre> <blockquote>

RFC 4648定义的“扩展Hex字符集”,用于DNS。

</blockquote> <pre><code class="lang-go hljs">var StdEncoding = NewEncoding(encodeStd) </code></code></pre> <blockquote>

RFC 4648定义的标准base32编码字符集。

</blockquote> <h3>type CorruptInputError</h3> <pre><code class="lang-go hljs">type CorruptInputError int64 </code></code></pre> <h4>func (CorruptInputError) Error</h4> <pre><code class="lang-go hljs">func (e CorruptInputError) Error() string </code></code></pre> <h3>type Encoding</h3> <pre><code class="lang-go hljs">type Encoding struct { encode [32]byte decodeMap [256]byte padChar rune } </code></code></pre> <blockquote>

双向的编码/解码协议,根据一个32字符的字符集定义,RFC 4648标准化了两种字符集。默认字符集用于SASI和GSSAPI,另一种用于DNSSEC。

</blockquote> <h4>func NewEncoding</h4> <pre><code class="lang-go hljs">func NewEncoding(encoder string) *Encoding </code></code></pre> <blockquote>

使用给出的字符集生成一个*Encoding,字符集必须是32字节的字符串。

</blockquote> <h4>func (*Encoding) DecodedLen</h4> <pre><code class="lang-go hljs">func (enc *Encoding) DecodedLen(n int) int </code></code></pre> <blockquote>

返回n字节base32编码的数据解码后的最大长度。

</blockquote> <h4>func (*Encoding) Decode</h4> <pre><code class="lang-go hljs">func (enc *Encoding) Decode(dst, src []byte) (n int, err error) </code></code></pre> <blockquote>

将src的数据解码后存入dst,最多写DecodedLen(len(src))字节数据到dst,并返回写入的字节数。如果src包含非法字符,将返回成功写入的字符数和CorruptInputError。换行符(\r、\n)会被忽略。

</blockquote> <h4>func (*Encoding) DecodeString</h4> <pre><code class="lang-go hljs">func (enc *Encoding) DecodeString(s string) ([]byte, error) </code></code></pre> <blockquote>

返回base32编码的字符串s代表的数据。

</blockquote> <pre><code class="lang-go hljs">func main() { str := "ONXW2ZJAMRQXIYJAO5UXI2BAAAQGC3TEEDX3XPY=" data, err := base32.StdEncoding.DecodeString(str) if err != nil { fmt.Println("error:", err) return } fmt.Printf("%q\n", data) } </code></code></pre> <h4>func (*Encoding) EncodedLen</h4> <pre><code class="lang-go hljs">func (enc *Encoding) EncodedLen(n int) int </code></code></pre> <blockquote>

返回n字节数据进行base32编码后的最大长度。

</blockquote> <h4>func (*Encoding) Encode</h4> <pre><code class="lang-go hljs">func (enc *Encoding) Encode(dst, src []byte) </code></code></pre> <blockquote>

将src的数据编码后存入dst,最多写EncodedLen(len(src))字节数据到dst,并返回写入的字节数。

函数会把输出设置为8的倍数,因此不建议对大数据流的独立数据块执行此方法,使用NewEncoder()代替。

</blockquote> <h4>func (*Encoding) EncodeToString</h4> <pre><code class="lang-go hljs">func (enc *Encoding) EncodeToString(src []byte) string </code></code></pre> <blockquote>

返回将src编码后的字符串。

</blockquote> <pre><code class="lang-go hljs">func main() { data := []byte("any old & data") str := base32.StdEncoding.EncodeToString(data) fmt.Println(str) } </code></code></pre> <h3>func NewDecoder</h3> <pre><code class="lang-go hljs">func NewDecoder(enc *Encoding, r io.Reader) io.Reader </code></code></pre> <blockquote>

创建一个新的base32流解码器。

</blockquote> <h3>func NewEncoder</h3> <pre><code class="lang-go hljs">func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser </code></code></pre> <blockquote>

创建一个新的base32流编码器。写入的数据会在编码后再写入w,base32编码每5字节执行一次编码操作;写入完毕后,使用者必须调用Close方法以便将未写入的数据从缓存中刷新到w中。

</blockquote> <pre><code class="lang-go hljs">func main() { input := []byte("foo\x00bar") encoder := base32.NewEncoder(base32.StdEncoding, os.Stdout) encoder.Write(input) // Must close the encoder when finished to flush any partial blocks. // If you comment out the following line, the last partial block "r" // won't be encoded. encoder.Close() } </code></code></pre> <h2>base64</h2> <blockquote>

base64实现了RFC 4648规定的base64编码。

</blockquote> <h3>Variables</h3> <pre><code class="lang-go hljs">var StdEncoding = NewEncoding(encodeStd) </code></code></pre> <blockquote>

RFC 4648定义的标准base64编码字符集。

</blockquote> <pre><code class="lang-go hljs">var URLEncoding = NewEncoding(encodeURL) </code></code></pre> <blockquote>

RFC 4648定义的另一base64编码字符集,用于URL和文件名。

</blockquote> <h3>type CorruptInputError</h3> <pre><code class="lang-go hljs">type CorruptInputError int64 </code></code></pre> <h4>func (CorruptInputError) Error</h4> <pre><code class="lang-go hljs">func (e CorruptInputError) Error() string </code></code></pre> <h3>type Encoding</h3> <pre><code class="lang-go hljs">type Encoding struct { encode [64]byte decodeMap [256]byte padChar rune strict bool } </code></code></pre> <blockquote>

双向的编码/解码协议,根据一个64字符的字符集定义,RFC 4648标准化了两种字符集。默认字符集用于MIME(RFC 2045)和PEM(RFC 1421)编码;另一种用于URL和文件名,用'-'和'_'替换了' '和'/'。

</blockquote> <h4>func NewEncoding</h4> <pre><code class="lang-go hljs">func NewEncoding(encoder string) *Encoding </code></code></pre> <blockquote>

使用给出的字符集生成一个*Encoding,字符集必须是64字节的字符串。

</blockquote> <h4>func (*Encoding) DecodedLen</h4> <pre><code class="lang-go hljs">func (enc *Encoding) DecodedLen(n int) int </code></code></pre> <blockquote>

返回n字节base64编码的数据解码后的最大长度。

</blockquote> <h4>func (*Encoding) Decode</h4> <pre><code class="lang-go hljs">func (enc *Encoding) Decode(dst, src []byte) (n int, err error) </code></code></pre> <blockquote>

将src的数据解码后存入dst,最多写DecodedLen(len(src))字节数据到dst,并返回写入的字节数。 如果src包含非法字符,将返回成功写入的字符数和CorruptInputError。换行符(\r、\n)会被忽略。

</blockquote> <h4>func (*Encoding) DecodeString</h4> <pre><code class="lang-go hljs">func (enc *Encoding) DecodeString(s string) ([]byte, error) </code></code></pre> <blockquote>

返回base64编码的字符串s代表的数据。

</blockquote> <pre><code class="lang-go hljs">func main() { str := "c29tZSBkYXRhIHdpdGggACBhbmQg77u/" data, err := base64.StdEncoding.DecodeString(str) if err != nil { fmt.Println("error:", err) return } fmt.Printf("%q\n", data) } </code></code></pre> <h4>func (*Encoding) EncodedLen</h4> <pre><code class="lang-go hljs">func (enc *Encoding) EncodedLen(n int) int </code></code></pre> <blockquote>

返回n字节数据进行base64编码后的最大长度。

</blockquote> <h4>func (*Encoding) Encode</h4> <pre><code class="lang-go hljs">func (enc *Encoding) Encode(dst, src []byte) </code></code></pre> <blockquote>

将src的数据编码后存入dst,最多写EncodedLen(len(src))字节数据到dst,并返回写入的字节数。

函数会把输出设置为4的倍数,因此不建议对大数据流的独立数据块执行此方法,使用NewEncoder()代替。

</blockquote> <h4>func (*Encoding) EncodeToString</h4> <pre><code class="lang-go hljs">func (enc *Encoding) EncodeToString(src []byte) string </code></code></pre> <blockquote>

返回将src编码后的字符串。

</blockquote> <pre><code class="lang-go hljs">func main() { data := []byte("any old & data") str := base64.StdEncoding.EncodeToString(data) fmt.Println(str) } </code></code></pre> <h3>func NewDecoder</h3> <pre><code class="lang-go hljs">func NewDecoder(enc *Encoding, r io.Reader) io.Reader </code></code></pre> <blockquote>

创建一个新的base64流解码器。

</blockquote> <h3>func NewEncoder</h3> <pre><code class="lang-go hljs">func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser </code></code></pre> <blockquote>

创建一个新的base64流编码器。写入的数据会在编码后再写入w,base32编码每3字节执行一次编码操作;写入完毕后,使用者必须调用Close方法以便将未写入的数据从缓存中刷新到w中。

</blockquote> <pre><code class="lang-go hljs">func main() { input := []byte("foo\x00bar") encoder := base64.NewEncoder(base64.StdEncoding, os.Stdout) encoder.Write(input) // Must close the encoder when finished to flush any partial blocks. // If you comment out the following line, the last partial block "r" // won't be encoded. encoder.Close() } </code></code></pre> 到此这篇关于“ Golang标准库——encoding(1)”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
常见的导致mysql中文乱码问题
更改MySQL数据库名实例代码
mysql中文乱码问题解决方法总结
linux下PostgreSQL的安装与使用
php实现简单用户登录功能程序代码
PHP中截取中文乱码解决办法
css中position相对定位和绝对定位(relative,absolute)详解
Go语言标准库学习——目录(汇总)
网页标题随机显示名言js代码
php excel操作类phpExcel用法介绍

[关闭]
~ ~