go binary


原文链接: go binary

注意 binary 包时,必须使用明确的长度确定的类型,可以用 (bool, int8, uint8, int16, float32, complex64, …) 但是不能用 int

有如下 go 代码:

const (
 foo = 123
)
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, foo)
fmt.Println(buffer.Len())

输出结果是 0,foo 没有写入到 buffer 中。原因在于,binary 包的 Write 内部调用了 intDataSize 函数获取 foo 的长度:

func intDataSize(data interface{}) int {
    switch data := data.(type) {
    case bool, int8, uint8, *bool, *int8, *uint8:
        return 1
    case []int8:
        return len(data)
    case []uint8:
        return len(data)
    case int16, uint16, *int16, *uint16:
        return 2
    case []int16:
        return 2 * len(data)
    case []uint16:
        return 2 * len(data)
    case int32, uint32, *int32, *uint32:
        return 4
    case []int32:
        return 4 * len(data)
    case []uint32:
        return 4 * len(data)
    case int64, uint64, *int64, *uint64:
        return 8
    case []int64:
        return 8 * len(data)
    case []uint64:
        return 8 * len(data)
    }
    return 0
}

而 foo 的类型是 int,intDataSize 函数不处理 int 类型,因此返回 0。导致没有数据被写入。
当然,这是我用错了,因为 binary 包的文档里面写了:

Numbers are translated by reading and writing fixed-size values.
A fixed-size value is either a fixed-size arithmetic
type (bool, int8, uint8, int16, float32, complex64, …)
or an array or struct containing only fixed-size values.

也就是说,使用 binary 包时,必须使用明确的长度确定的类型,可以用 int32,但别用 int。
没看清除文档,用错了,怨不得别人。但是 binary 包把错误直接吞掉的做法,似乎和 go 的错误处理哲学背离了啊。
intDataSize 函数对于他不能处理的类型,难道不能返回一个 error 么?或者粗暴些直接给个 panic 也行啊。这样至少能快速定位错误吧。否则对于不习惯看文档,只是看一下方法的声明就想快速使用的人,这种坑掉进去要爬出来是颇费一些工夫的。

`