Access the C structure field with the name reserved in Go.

problem

I want to refer to the field named type, but I get the following error because it is reserved in Go.

package main

/*
typedef struct {
	int type;
} Struct;
*/
import "C"

import "fmt"

func main() {
  test := C.Struct{1}
  fmt.Println(test.type)
}

error


./Main.go:14:20: expected selector or type assertion, found 'type'

solution

You can refer to it by adding an underscore to the field name when referencing.

package main

/*
typedef struct {
	int type;
} Struct;
*/
import "C"

import "fmt"

func main() {
  test := C.Struct{1}
  fmt.Println(test._type) //Not type_See by type.
}

output


1

reference

Recommended Posts

Access the C structure field with the name reserved in Go.
Access MongoDB in C
Load the module with the same name in another location
When the variable name conflicts with the Devaga command in pdb
Using cgo with the go command
Get the host name in Python
Access the Twitter API in Python
Segfault with 16 characters in C language
Memorandum (Add name only to people with the same surname in the list)
Replace the directory name and the file name in the directory together with a Linux command.
Behavior when returning in the with block
Implement part of the process in C ++
[Python] Get the variable name with str
Display Python 3 in the browser with MAMP
I wrote the selection sort in C
When changing the table name with flask_migrate
Communicate with I2C devices in Linux C
Function to extract the maximum and minimum values ​​in a slice with Go
Make the library created by Eigen in C ++ available from Python with Boost.Numpy.
[Spoiler note] Go to predict the bride in five equal parts with PyTorch