我有这个代码:
hashChannel <- []byte(md5.Sum(buffer.Bytes()))
我得到了这个错误:
cannot convert md5.Sum(buffer.Bytes()) (type [16]byte) to type []byte
即使没有显式转换,这也不起作用.我也可以保留类型[16]byte,但在某些情况下需要转换它,因为我要通过TCP连接发送它:
_, _ = conn.Write(h)
转换它的最好方法是什么? 谢谢
我有这个代码:
hashChannel <- []byte(md5.Sum(buffer.Bytes()))
我得到了这个错误:
cannot convert md5.Sum(buffer.Bytes()) (type [16]byte) to type []byte
即使没有显式转换,这也不起作用.我也可以保留类型[16]byte,但在某些情况下需要转换它,因为我要通过TCP连接发送它:
_, _ = conn.Write(h)
转换它的最好方法是什么? 谢谢
对数组进行切片.例如,
package main
import (
"bytes"
"crypto/md5"
"fmt"
)
func main() {
var hashChannel = make(chan []byte, 1)
var buffer bytes.Buffer
sum := md5.Sum(buffer.Bytes())
hashChannel <- sum[:]
fmt.Println(<-hashChannel)
}
输出:
[212 29 140 217 143 0 178 4 233 128 9 152 236 248 66 126]