cgo: howto refer to a C struct field named 'type'

1077 views
Skip to first unread message

Sonia Hamilton

unread,
Jan 28, 2013, 8:42:35 PM1/28/13
to golan...@googlegroups.com
How do I refer to a C struct field named 'type', given that 'type' is a
reserved word in Go? For example, in this struct:

> struct _GNetSnmpVarBind {
> guint32 *oid; /* name of the variable */
> gsize oid_len; /* length of the name */
> GNetSnmpVarBindType type; /* variable type / exception */
> ...
> };

For example, if the struct is referred to by data,
fmt.Println(data.type) gives:

"expected selector or type assertion, found 'type'"

--
Sonia Hamilton
http://www.snowfrog.net

minux

unread,
Jan 28, 2013, 9:44:09 PM1/28/13
to golan...@googlegroups.com
i think it's impossible to reference that field directly, you'd have to call a
C wrapper function to do that. 

Sonia Hamilton

unread,
Jan 28, 2013, 11:38:31 PM1/28/13
to golan...@googlegroups.com
* Sonia Hamilton <so...@snowfrog.net> [2013-01-29 12:42:34 +1100]:

> How do I refer to a C struct field named 'type', given that 'type' is a
> reserved word in Go? For example, in this struct:
>
> > struct _GNetSnmpVarBind {
> > guint32 *oid; /* name of the variable */
> > gsize oid_len; /* length of the name */
> > GNetSnmpVarBindType type; /* variable type / exception */
> > ...
> > };
>
> For example, if the struct is referred to by data,
> fmt.Println(data.type) gives:

Using the power of documentation, one finds [1]:

> Within the Go file, C identifiers or 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

[1] http://golang.org/cmd/cgo/

汪培

unread,
Mar 23, 2023, 2:24:22 PMMar 23
to golang-nuts

I just came across the same issue and thanks for your record, I just solve my problem.
Reply all
Reply to author
Forward
0 new messages