Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect behavior with cgo when only import "C" is used #156

Open
kokororin opened this issue May 27, 2024 · 0 comments
Open

Incorrect behavior with cgo when only import "C" is used #156

kokororin opened this issue May 27, 2024 · 0 comments

Comments

@kokororin
Copy link

When using cgo, the behavior can be incorrect if there is only an import "C" statement in the Go file.

Before:

package demo

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

// Version returns version string
func Version() string {
	return C.GoString(C.version())
}

After:

package demo

import "C"

func Version() string {
	return C.GoString(C.version())
}

One workaround for this issue is to add an underscore import after import "C". This ensures the correct behavior. Here is the modified code:

package demo

/*
#include <stdlib.h>
#include "version.h"
*/
import "C"
import (
	_ "fmt"
)

// Version returns version string
func Version() string {
	return C.GoString(C.version())
}
@kokororin kokororin changed the title cgo comments will be removed Incorrect behavior with cgo when only import "C" is used May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant