从Go中访问C结构字段:使用下划线还是不使用下划线?

huangapple go评论94阅读模式
英文:

cgo C struct field access from Go: underscore or no underscore?

问题

我在访问GO代码中的C结构体时,发现在线文档和程序行为之间存在不一致。我的go version显示我正在使用的版本是:

go version go1.4.2 linux/amd64

根据GO CGO文档

> 在Go文件中,可以通过在关键字前加下划线来访问C结构体字段的名称:如果x指向一个具有名为"type"的字段的C结构体,x._type将访问该字段。无法在Go中表示的C结构体字段,例如位字段或不对齐的数据,在Go结构体中被省略,用适当的填充替换以达到下一个字段或结构体的末尾。

我在这方面遇到了问题,所以我编写了一个快速的示例程序来测试它:

package main
// struct rec
// {
//      int    i;
//      double d;
//      char*  s;
// };
import "C"
import "fmt"
func main() {
        s := "hello world"
        r := C.struct_rec{}
        r.i = 9
        r.d = 9.876
        r.s = C.CString(s)
        fmt.Printf("\n\tr.i: %d\n\tr.d: %f\n\tr.s: %s\n", 
                r.i, 
                r.d, 
                C.GoString(r.s))
}

当我按照文档中所示使用下划线(例如,将r._i替换为r.i)时,我会得到以下编译错误:

> r._i undefined (type C.struct_rec has no field or method _i)

当我不使用下划线时,它可以正常工作。我尝试过使用指针和非指针。我唯一能想到的其他可能是,也许是因为我在GO中分配了实例而不是在C中,这样吗?

谢谢您的帮助!

英文:

I'm running into a disconnect between the online documentation and the behavior I see in my programs accessing C structs within GO code. go version says I am using:

go version go1.4.2 linux/amd64

According to the GO CGO documentation:

> Within the Go file, C's struct field names that are keywords in Go can be
> accessed by prefixing them with an underscore: if x points at a C struct with
> a field named "type", x._type accesses the field. C struct fields that cannot
> be expressed in Go, such as bit fields or misaligned data, are omitted in the
> Go struct, replaced by appropriate padding to reach the next field or the end
> of the struct.

I had troubles with this, so made a quick sample program to test it out:

package main
// struct rec
// {
//      int    i;
//      double d;
//      char*  s;
// };
import "C"
import "fmt"
func main() {
        s := "hello world"
        r := C.struct_rec{}
        r.i = 9
        r.d = 9.876
        r.s = C.CString(s)
        fmt.Printf("\n\tr.i: %d\n\tr.d: %f\n\tr.s: %s\n", 
                r.i, 
                r.d, 
                C.GoString(r.s))
}

When I use underscores as the docs indicate (eg, substitute r._i for r.i above) I get the following compile error:

> r._i undefined (type C.struct_rec has no field or method _i)

When I don't use underscores it works fine. I tried this with both pointers and non-pointers. The only other idea I can think of is that maybe it's because I allocated the instances in GO rather than C, is that the case??

Thanks for any help!

答案1

得分: 5

答案就在你问题中引用的那句话中:

在Go文件中,可以通过在C的结构字段名前加下划线来访问那些在Go中是关键字的C结构字段名(参考链接:http://golang.org/ref/spec#Keywords)。

ids在Go中不是关键字。

英文:

The answer is in the very quote you have in your question:

>Within the Go file, C's struct field names that are keywords in Go can be accessed by prefixing them with an underscore(…)

i, d, and s are not keywords in Go.

huangapple
  • 本文由 发表于 2015年7月1日 22:15:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/31163866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定