Unable to export go function with const *C.char parameter

Viewed 371

I have the requirement constraint for my work, due to which I need to use a C++ function as:

bool f1(const char* str)
{
    //.....
}

The function argument needs to be type const char*

However, when I try something like following in the golang source code:

//export f1
func f1(str const *C.char) C.bool { 
    //.......
}

On doing go build, I get the error:

expected type, found 'const'

I tried replacing

func f1(str const *C.char)

by

func f1(str const *C.GoString)

This time, I got the error:

expression C.GoString used as type

I am clueless about how to do so.

1 Answers

You can't use const directly. You have to create a typedef of const char . And then use that type into your function argument.

Example

package main

/*
#include<stdbool.h>
typedef const char cchar_t;
*/
import "C"

//export f1
func f1(str *C.cchar_t) C.bool {
    return false
}

func main() {}
Related