英文:
Converting C array and pointer code to Go
问题
我正在尝试将一个C代码重写为Go,我注意到要重写的代码遵循了一个常见的模式。许多函数接受unsigned char
数组作为指针传递,并且在调用函数时或者函数的其他部分,我们会对char
指针进行一些值的加法运算。这里只是为了演示目的给出一个简单的示例。
static void func1(unsigned char *r) {
int i;
for (i = 0; i < CONSTANT; i++)
func2(r + i * OTHER_CONSTANT);
}
static void func2(const unsigned char *a) {
unsigned int i;
for (i = 0; i < CONSTANT; i += 8) {
// do some stuff
a += 3;
}
}
如何将这样的代码转换为Go的正确方式?我开始使用byte
切片来表示char
数组,但我不知道如何将这些涉及char
的算术表达式准确地转换为Go中的范围/索引算术。我最终得到了以下代码:
func func1(r []byte) {
for i := 0; i < CONSTANT; i++ {
func2(r + i*OTHER_CONSTANT) // 这样是行不通的
}
}
func func2(a []byte) {
for i := 0; i < CONSTANT; i += 8 {
// 做一些操作
a += 3 // 这样也是行不通的
}
}
有没有办法将上述的C代码转换为Go代码?
英文:
I'm trying to rewrite a C code in Go, and what I notice is that the code that I try to rewrite follows a lot one common pattern. Many of the functions accept unsigned char
arrays, passed as pointer, and either when calling the functions or at some other part of the function we add some value to the char
pointer, as doing arithmetic. Here is a simple example just for demonstration purposes.
static void func1(unsigned char *r) {
int i;
for (i = 0; i < CONSTANT; i++)
func2(r + i * OTHER_CONSTANT);
}
static void func2(const unsigned char *a) {
unsigned int i;
for (i = 0; i < CONSTANT; i += 8) {
// do some stuff
a += 3;
}
}
What is a proper way to convert something like this to Go? I started to use byte
slices in order to represent the char
arrays, but I don't know how to exactly transfer those arithmetic expression with char
into arithmetic with ranges/indexes in Go. I ended up having something like this:
func func1(r []byte) {
for i := 0; i < CONSTANT; i++ {
func2(r + i*OTHER_CONSTANT) // This just doesn't work
}
}
func func2(a []byte) {
for i := 0; i < CONSTANT; i += 8 {
// do some stuff
a += 3 // This doesn't work
}
}
Any idea how to convert something like above from C to Go?
答案1
得分: 2
你想要对 r
和 a
进行切片操作,可以按照以下方式进行:
func func1(r []byte) {
for i := 0; i < CONSTANT; i++ {
func2(r[i*OTHER_CONSTANT:])
}
}
func func2(a []byte) {
for i := 0; i < CONSTANT; i += 8 {
// 做一些操作
a = a[3:]
}
}
以上是对代码进行的翻译。
英文:
You want to be slicing r
and a
, which you can do in the following way:
func func1(r []byte) {
for i := 0; i < CONSTANT; i++ {
func2(r[i*OTHER_CONSTANT:])
}
}
func func2(a []byte) {
for i := 0; i < CONSTANT; i += 8 {
// do some stuff
a = a[3:]
}
}
答案2
得分: 1
Go语言不支持指针算术运算:
https://golang.org/doc/faq#no_pointer_arithmetic
因此,你应该使用指向数组位置的索引来操作,使用切片来获取数组的部分:
func func1(r []byte) {
rIndex := 0
for i := 0; i < CONSTANT; i++ {
func2(r[rIndex + i*OTHER_CONSTANT:])
}
}
还有:
func func2(a []byte) {
aIndex := 0
for i := 0; i < CONSTANT; i += 8 {
// 做一些操作
aIndex += 3
// 需要时使用 a[aIndex]
}
}
英文:
Go does not have pointer arithmetic:
https://golang.org/doc/faq#no_pointer_arithmetic
So you should just work with indexes that point to positions on the array, and slices to get parts of the array:
func func1(r []byte) {
rIndex := 0
for i := 0; i < CONSTANT; i++ {
func2(r[rIndex + i*OTHER_CONSTANT:])
}
}
And:
func func2(a []byte) {
aIndex := 0
for i := 0; i < CONSTANT; i += 8 {
// do some stuff
aIndex += 3
// use a[aIndex] when needed
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论