Could not determine kind of name for C.free ?!

Hello everyone,
I’m unable to compile the following code:

package main
import "C"

/*
#include <stdlib.h>
*/

//export HelloFromGo
func HelloFromGo() *C.char {
  str := C.CString("Hello World")
  defer C.free(unsafe.Pointer(str))
  return str
}

func main() {}

It bails out with could not determine kind of name for C.free , already included stdlib, but still cannot compile the code with C.free

1 Like

The comments related to import "C" need to go above the import statement. Also, unsafe needs to be imported.

package main

/*
#include <stdlib.h>
*/
import "C"
import "unsafe"

//export HelloFromGo
func HelloFromGo() *C.char {
	str := C.CString("Hello World")
	defer C.free(unsafe.Pointer(str))
	return str
}

func main() {}
3 Likes

Also, it looks like you’re returning a free’d pointer which is probably a bad idea.

3 Likes

Thank you guys!

@calmh will make another function to “free” the pointer.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.