Label: cgo

cgo is a tool that allows Go code to interact with C libraries and native APIs. It is included as part of the Go toolchain. Developers use cgo when pure Go code cannot access required low-level functionality. The tool generates bridge code between Go and C automatically. Go functions can call C code directly through special import directives. C code can also invoke exported Go functions. Memory management and type conversion require special care because Go and C use different runtimes. cgo is commonly used for operating system APIs, existing native libraries, and performance-critical components. The feature increases interoperability but also adds build complexity and platform dependencies.

Understanding cgo is important for systems programming and cross-language integration. It is widely used in networking, security, graphics, and low-level tooling.

String transfer from a function in GO to a code in C without memory allocation (Part 2)

We were discussing a possibility of string transfer without memory allocation in the previous article. Attempts to use this method in the real projects revealed that there are cases when this method is not applicable. Everything is working just fine if a string object is constructed from a static string constant or by means of

String transfer from a function in GO to a code on C without memory allocation (Part 1)

CGO documentation illuminates string transfer issue rather poorly. They only mention that C.CString() function should be used in order to convert a string object into a pointer to a buffer with a zero-terminated string – char*, which is coherent for the C code. This is great; however, a memory block is being allocated during this procedure