package main
/*
#include <stdlib.h>
typedef struct Person {
char* name;
int age;
} Person;
*/
import "C"
import (
"fmt"
"unsafe"
)
func (p *C.Person) PrintName() {
name := C.GoString(p.name)
fmt.Println(name)
}
func main() {
name := C.CString("John")
defer C.free(unsafe.Pointer(name))
age := C.int(30)
cPerson := C.Person{
name: name,
age: age,
}
person := C.Person(cPerson)
person.PrintName()
}
The above program runs and compiles perfectly fine. However, while using Code Editors (Vs Code or GoLand) you can see the above issue. It doesn't recognise PrintName() function. Moreover, if I run gopls check main.go
I get the following response:
main.go:34:9-18: person.PrintName undefined (type _Ctype_struct_Person has no field or method PrintName)
Does anyone have any idea how to resolve this? Clearly the issue is with gopls. Code refactoring can resolve this issue but it is not a suitable option for existing large code base.