Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Golang-将int添加到字节数组的末尾_Go - Fatal编程技术网

Golang-将int添加到字节数组的末尾

Golang-将int添加到字节数组的末尾,go,Go,我试图在Golang中的字节数组末尾添加一个int。 这是我当前的代码: nameLengthBytes := []byte{32, 32} nameLength := len(name) namelingthbytes创建了两个空格,我正在寻找的是一种将namelingthbytes添加到namelingthbytes末尾的方法 示例: 如果名称长度为7,我希望数组为:{32,55} 如果名称长度是12,我希望数组是{49,50} 问题是有时名称短于10,所以我需要用前导零填充 您想要以字节

我试图在Golang中的字节数组末尾添加一个int。
这是我当前的代码:

nameLengthBytes := []byte{32, 32}
nameLength := len(name)
namelingthbytes创建了两个空格,我正在寻找的是一种将namelingthbytes添加到namelingthbytes末尾的方法

示例:
如果名称长度为7,我希望数组为:{32,55}
如果名称长度是12,我希望数组是{49,50}


问题是有时名称短于10,所以我需要用前导零填充

您想要以字节形式对数字进行空格填充的ascii表示吗
fmt.Sprintf
生成一个字符串,然后可以将其转换为字节

这里有一些代码,或者


我不确定我是否理解你的问题?您是如何尝试添加前导零的?什么导致问题?哦,对不起,忘了提那个。我没有添加任何关于添加号码的代码,因为我所有的尝试都失败了。
package main

import "fmt"

func main() {
    bs := []byte(fmt.Sprintf("%2d", 7))
    fmt.Println(bs)
}