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

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

问题描述

我正在尝试将固定大小的数组 [32]byte 转换为可变大小的数组(切片)[]byte:

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

package main

import (
        "fmt"
)

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

但编译器会抛出错误:

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

我应该如何转换它?

推荐答案

使用 b := a[:] 获取您拥有的数组的切片.另请参阅 this 博客文章,了解有关数组和切片的更多信息.

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天全站免登陆