英文:
Python bytes() or bytesarray() counterpart in golang
问题
我正在寻找将一些Python代码转换为Go的方法。Python代码如下所示:
to_be_converted = [3, 40, 234, 1, 23, 65, 43, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
converted = bytes(to_be_converted)
print(converted)
这将输出:
b'\x03(\xea\x01\x17A+Hello world'
我正在寻找一种在Go中获取这个字节对象的方法。我不介意输入数据是否不同,我只是想找到获取输出的方法。谢谢。
英文:
I am looking to port some code from python into golang.
The code in python goes like this
to_be_converted = [3, 40, 234, 1, 23, 65, 43, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
converted = bytes(to_be_converted)
print(converted)
this results to
b'\x03(\xea\x01\x17A+Hello world'
I am looking for a way to get this bytes object in golang.
I dont mind if the input data is different, I am only looking for a way to obtain the output.
Thank you
答案1
得分: 3
Go语言有一个内置的byte类型,你可以像这样创建byte数组:
myBytes := []byte{3, 40, 234, 1, 23, 65, 43, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100}
要获取字符串输出,你可以将byte数组转换为字符串,像这样:
myString := string(myBytes)
fmt.Println(myString) // 输出 (�A+Hello world
英文:
Go has a builtin byte type and you can create byte arrays like this:
myBytes := []byte{3, 40, 234, 1, 23, 65, 43, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100}
To get the string output, you can just convert the byte array to a string like this:
myString := string(myBytes)
fmt.Println(myString)// prints (�A+Hello world
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论