doulangpeng3933 2016-05-17 21:50
浏览 246
已采纳

Go Base64软件包中的“ StdEncoding”是什么

I'm an experienced programmer, but new to go, so apologies in advance for what I'm sure is an easy question.

The base64 example code on the goland.org site includes the following code.

package main

import (
    "encoding/base64"
    "fmt"
)

func main() {
    msg := "Hello, 世界"
    encoded := base64.StdEncoding.EncodeToString([]byte(msg))
    fmt.Println(encoded)
    decoded, err := base64.StdEncoding.DecodeString(encoded)
    if err != nil {
        fmt.Println("decode error:", err)
        return
    }
    fmt.Println(string(decoded))
}

I can copy and use this code without issue, but as a new go programmer, I don't understand what that StdEncoding part of the following lines does

 decoded, err := base64.StdEncoding.DecodeString(encoded)

I understand the above program imports the encode/base64 functions into my program. However, if I look at the documentation, the base64 package consists of the following types and functions

func NewDecoder(enc *Encoding, r io.Reader) io.Reader
func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser
type CorruptInputError
    func (e CorruptInputError) Error() string
type Encoding
    func NewEncoding(encoder string) *Encoding
    func (enc *Encoding) Decode(dst, src []byte) (n int, err error)
    func (enc *Encoding) DecodeString(s string) ([]byte, error)
    func (enc *Encoding) DecodedLen(n int) int
    func (enc *Encoding) Encode(dst, src []byte)
    func (enc *Encoding) EncodeToString(src []byte) string
    func (enc *Encoding) EncodedLen(n int) int
    func (enc Encoding) WithPadding(padding rune) *Encoding

I would expect to see a type of StdEncoding -- but I don't see any mention of a StdEncoding in this list.

So, my immediate question is -- what is StdEncoding?

My higher level question is probably -- what, exactly, is exported from a module for consumption by a user of that module, and how can I, as a go user, browse those exported things.

Bonus points if you can correct any unwitting misuse of terms like module, export, import above and put them into a go context.

  • 写回答

1条回答 默认 最新

  • doulin6088 2016-05-17 22:04
    关注

    To try and answer all of your questions at once... You can find the definition of StdEncoding in the file base64.go it's contained here;

    65  // StdEncoding is the standard base64 encoding, as defined in
    66  // RFC 4648.
    67  var StdEncoding = NewEncoding(encodeStd)
    

    and up on line 33 that const is defined as such;

    33  const encodeStd = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
    

    So to answer your main question, it is just base64 encoding as defined in RFC 4648. The type is *Encoding and you can even view the implementation of NewEncoding in the same location if you'd like. https://golang.org/src/encoding/base64/base64.go

    To address some of the auxiliary questions about packages/modules and exporting... In the go docs you'll see everything that is exported and nothing that is unexported (if you look at the actual source you'll usually find a good deal of unexported helper methods). If you were looking at the source you can see this distinction in the names of methods/types/consts ect. The basic rule is, if the identifier starts with a capital letter then it is exported, if it's lower cased then it is not exported.

    One other thing worth pointing out is that is that the language is open source. In the docs it will typically list the packages main file (possibly others as well) so you can open it up and have a look. You'll find the code is well commented and usually pretty easy to understand. Often times you'll find answers to questions about how things work internally just by reading the comments in these files.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题