转换固定大小的数组中GO可变大小的数组 [英] Convert fixed size array to variable sized array in GO

查看:505
本文介绍了转换固定大小的数组中GO可变大小的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转换一个固定大小的数组[32]字节可变大小的数组[]字节:

I'm trying to convert a fixed size array [32]byte to variable sized array []byte:

package main

import (
        "fmt"
)

func main() {
        var a [32]byte
        b := []byte(a)
        fmt.Println(" %x", b)
}

但是编译器会引发错误:

but the compiler throws the error:

./test.go:9: cannot convert a (type [32]byte) to type []byte

我应该如何转换呢?

How should I convert it?

推荐答案

使用 B:= A [:] 来让你有阵列上的切片。另请参见这个的博客张贴有关数组和切片的更多信息。

Use b := a[:] to get the slice over the array you have. Also see this blog post for more information about arrays and slices.

这篇关于转换固定大小的数组中GO可变大小的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆