Questions tagged [cgo]

Cgo enables the creation of Go packages that call C code.

Cgo enables the creation of Go packages that call C code.

If a Go source file imports "C", it is using cgo. The Go file will have access to anything appearing in the comment immediately preceding the line import "C", and will be linked against all other cgo comments in other Go files, and all C files included in the build process.

Note that there must be no blank lines in between the cgo comment and the import statement.

To access a symbol originating from the C side, use the package name C. That is, if you want to call the C function printf() from Go code, you write C.printf().

It is possible to call both top-level Go functions and function variables from C code invoked from Go code using cgo.

Go makes its functions available to C code through use of a special //export comment. Note: you can't define any C functions in preamble if you're using exports.

Source:https://github.com/golang/go/wiki/cgo

846 questions
-1
votes
1 answer

C call Go exported function

I want to return an array to C caller, just like below, how to do it? //export EtcdGetAllNodes func EtcdGetAllNodes()[]uint32 { a := []uint32{1,2,3} return a } This function EtcdGetAllNodes will try to get value with a prefix for the…
dingrui
  • 417
  • 2
  • 5
  • 15
-1
votes
2 answers

Include libsodium using cgo in go project fails

I am trying to include the libsodium into my Go project. For that, I've copied the repo inside my project // #cgo CFLAGS: -I/mypath/libsodium/src/libsodium/include/sodium // #include // #include "crypto_sign_ed25519.h" import "C" When…
manolodewiner
  • 504
  • 3
  • 26
-1
votes
1 answer

import c-shared library that generated by cython to go with cgo

I wanna import a c-shared-library to go that generated by Cython in python 3.7, try do it by cgo. in this case: go version go1.12.7 linux/amd64 Python 3.7.3 Cython version 0.29.12 os: Manjaro 18.0.4 Kernel: x86_64 Linux 5.1.19-1 I will…
hamid ghadery
  • 31
  • 1
  • 5
-1
votes
1 answer

Direct C pointer conversion

I have this C code: uint8_t *data[BUF_SIZE]; data = ...; // extern void goReadData(uint8_t *data, int bufferSize); goReadData(data, BUF_SIZE) And in the GO Code I'm trying to use the data pointer as a GO array or slice, I want to retrieve an…
Vincent Em
  • 11
  • 3
-1
votes
1 answer

Use Go .Variable inside range block in THTML file

I have a .thtml file: ...

{{.Something}}

{{range ...}}

{{.Something}}

{{end}}
... If I use the value of .Something inside the .thtml…
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
-1
votes
1 answer

cgo - 'runtime.h' file not found in MacOSX

I am trying to execute the cgo program as mentioned here package main /* #include "runtime.h" int goId() { return g->goid; } */ import "C" import "fmt" func main() { x := C.goId() …
tuk
  • 5,941
  • 14
  • 79
  • 162
-1
votes
2 answers

Copy data file within Go package?

I have a Go package that links to C library using CFLAGS for cgo. C library, in turn, requires local data file to work properly. It's prebuilt 3d-party library and option to set data file path is not available. Everything works as expected if client…
Anatolii
  • 1
  • 1
-1
votes
2 answers

Cgo: How to return double array from C to Go

I got a C function like this double* c_func(int n_rows) { double result[n_rows]; for (int i = 0; i < n_rows; ++i) { result[i] = (double)i; } return result; } And I use this Go function to process the C double: // convert C double…
lazywei
  • 11,955
  • 5
  • 20
  • 25
-2
votes
1 answer

Analyzing a crash from a rust-generated C library

I needed to call Rust code from my Go code. Then I used C as my interface. I did this: I've created a Rust library that takes a CStr as a parameter, and returns a new processed string back as CStr. This code is statically compiled to a static C…
Magmus
  • 207
  • 2
  • 11
-2
votes
1 answer

Building a server that can run every where, desktop and mobile

I've the below simple go server that is running at my laptop (Mac/Windows/Linux): package main import ( "fmt" "log" "net/http" ) func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hi there %s!",…
Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203
-2
votes
1 answer

Go equivalent of void** from C

I need to call a C function which takes an argument of type void** from my Go code. What is the Go equivalent of void**? And how to properly pass it to the C function?
zergon321
  • 210
  • 1
  • 10
-2
votes
1 answer

Is there a way to pass a TagLib::PropertyMap to Go?

I'm trying to get the values from TagLib::PropertyMap into Go. I've seen some ways that work in getting the values by looping over k/v pairs but is that the most efficient way to do that? Shouldn't I just be able to return PropertyMap from C++ and…
oonebaddog
  • 23
  • 5
-2
votes
1 answer

how to write comments in cgo arguments

I have the following cgo arguments and I want to annotate lines with things like // this checks for 32bit etc. how can I do that without getting? invalid flag in #cgo CFLAGS: // // #cgo CFLAGS: -I./depend/ -I./depend/src/ // #cgo 386 amd64p32 arm…
elichai2
  • 1,365
  • 3
  • 14
  • 28
-2
votes
1 answer

How do I correctly use 64-bit TDM-GCC with cgo?

I'm attempting to use a library at github.com/hajimehoshi/ebiten. Regardless of what I do, I'm met with this error: # github.com/go-gl/glfw/v3.2/glfw cc1.exe: sorry, unimplemented: 64-bit mode not compiled in # github.com/go-gl/gl/v2.1/gl cc1.exe:…
Orange Receptacle
  • 1,173
  • 3
  • 19
  • 40
-3
votes
1 answer

cgo: exec gcc: exec: "gcc": executable file not found in $PATH Error in Egress operator installation

I am trying Egress Operator for restricting the egress calls based on domain. I am using Ubuntu 18 Hyper-V VM and I have all prerequisites: Azure repository to push images Kubebuilder for code generation Kustomize for building the Kubernetes…
solveit
  • 869
  • 2
  • 12
  • 32
1 2 3
56
57