Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cgo: cannot access struct field called type #25458

Closed
jech opened this issue May 18, 2018 · 4 comments
Closed

Cgo: cannot access struct field called type #25458

jech opened this issue May 18, 2018 · 4 comments

Comments

@jech
Copy link

jech commented May 18, 2018

go version go1.10.1 linux/amd64

I'm using a C library that manipulates a struct that has a field called type. I see no way to access the field without going through a wrapper function written in C.

Here's a fairly minimal example that reproduces the problem:

/*
struct foo {
    int type;
};
*/
import "C"

func foo_type (x *C.struct_foo) int
    return int(x.type);
}
@AlexRouSg
Copy link
Contributor

https://golang.org/cmd/cgo/

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.

@agnivade
Copy link
Contributor

Working as intended.

@jech
Copy link
Author

jech commented May 18, 2018 via email

@jech
Copy link
Author

jech commented May 18, 2018 via email

@golang golang locked and limited conversation to collaborators May 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants