douying9296 2017-08-29 20:59
浏览 213
已采纳

Base64编码/解码导致输出损坏

I'm trying to write some convenience wrapper funcs that base64 encodes and decodes byte slices. (Can't understand why this is not conveniently provided in the stdlib.)

However this code (in playground):

func b64encode(b []byte) []byte {
    encodedData := &bytes.Buffer{}
    encoder := base64.NewEncoder(base64.URLEncoding, encodedData)
    defer encoder.Close()
    encoder.Write(b)
    return encodedData.Bytes()
}

func b64decode(b []byte) ([]byte, error) {
    dec := base64.NewDecoder(base64.URLEncoding, bytes.NewReader(b))
    buf := &bytes.Buffer{}
    _, err := io.Copy(buf, dec)
    if err != nil {
        return nil, err
    }
    return buf.Bytes(), nil
}


func main() {
    b := []byte("hello")
    e := b64encode(b)
    d, err := b64decode(e)
    if err != nil {
        log.Fatalf("could not decode: %s", err)
    }
    fmt.Println(string(d))
}

generates truncated output when I try to print it:

hel

What's going on?

  • 写回答

1条回答 默认 最新

  • duankan8739 2017-08-29 21:02
    关注

    The defer executes when the function ends. That is AFTER the return statement has been evaluated.

    The following works: https://play.golang.org/p/sYn-W6fZh1

    func b64encode(b []byte) []byte {
        encodedData := &bytes.Buffer{}
        encoder := base64.NewEncoder(base64.URLEncoding, encodedData)
        encoder.Write(b)
        encoder.Close()
        return encodedData.Bytes()
    }
    

    That being said, if it really is all in memory, you can avoid creating an encoder entirely. Instead, you can do something like:

    func b64encode(b []byte) []byte {
        ret := make([]byte, base64.URLEncoding.EncodedLen(len(b)))
        base64.URLEncoding.Encode(ret, b)
        return ret
    }
    

    An added benefit of doing it this way it it is more efficient since it only needs to allocate once. It also allows you to no longer ignore errors in the Write and Close methods.

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

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64