問題
typeという名前のフィールドを参照したいがGoに予約されているため下記のようなエラーがでる。
package main
/*
typedef struct {
int type;
} Struct;
*/
import "C"
import "fmt"
func main() {
test := C.Struct{1}
fmt.Println(test.type)
}
エラー
./Main.go:14:20: expected selector or type assertion, found 'type'
解決策
参照時にフィールド名にアンダーバーを付けることで参照できるようになる。
package main
/*
typedef struct {
int type;
} Struct;
*/
import "C"
import "fmt"
func main() {
test := C.Struct{1}
fmt.Println(test._type) // typeではなく_typeで参照。
}
出力
1