2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Goで予約された名前のCの構造体フィールドにアクセスする。

Posted at

問題

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

参考

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?