小编Ang*_*gel的帖子

如何在 Go 中编写 LEB128

你如何在 Go 中将整数写入 LEB128 格式?我正在尝试将 int32 编码为Minecraft VarInt,到目前为止我已经尝试将 wiki 上的示例导入到 Go。我在测试时得到了错误的结果,wiki 说 -1 应该等于 [255 255 255 255 15],但我得到的是 [255 255 255 255 255]。我在这里做错了什么?

func WriteVarInt2(v int32) []byte{
   var out []byte
   c := 0
   for{
       currentByte := byte(v & 0b01111111)
       v >>= 7
       if v != 0 {
           currentByte |= 0b10000000
       }
       out = append(out, currentByte)
       c++

       if c >= 5 || v == 0{
           return out
       }
    }
}
Run Code Online (Sandbox Code Playgroud)

binary go varint

5
推荐指数
1
解决办法
176
查看次数

控制台中的 Go 和颜色

如何让我的 Go 程序在控制台中打印彩色文本而不使用外部库?

我试过了:

fmt.Print("\033[33m"+mystring)
Run Code Online (Sandbox Code Playgroud)

但它不产生任何颜色。

command-line-interface go

4
推荐指数
1
解决办法
8391
查看次数

标签 统计

go ×2

binary ×1

command-line-interface ×1

varint ×1