Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Is it possible to replace C and C++ with Go + cgo and interface frameworks such

ID: 652402 • Letter: I

Question

Is it possible to replace C and C++ with Go + cgo and interface frameworks such as Qt or DirectX with Go?

Any potential pitfalls I should be aware of?

UPDATE:

I see that my question was not precise enough. Of course I expected problems with Go concepts that aren't available in C/C++ e.g. goroutines. What I really wanted to know was if you could use Go to replace standard C++ functionality. I hope I can convey my idea: I thought of Go as a "scripting language" to leverage C/C++ libraries with a new language.

I'd like to know:

Can you register Go methods as C/C++ callbacks ?
Can I supply Go variables as parameters to C/C++ functions ?
Are their pointers compatible ? Go runs on a native Vm, so I guessed there might be an abstraction layer over the memory that invalidates C/C++ pointers
Have you ever used cgo or know about its abilities ?

Explanation / Answer

Go has a simple interface to C libraries: cgo. It has no support for C++ (classes, name mangling). An alternative to cgo is Swig.

Can you register Go methods as C/C++ callbacks ?

It is possible to call Go functions (not methods) from C. A simple example can be found here.

It is hard to understand what the limitations of calling C from Go are.

Cgo has currently no support for generating standalone Go libraries (.a or .so) for use from C code. Go programs are able to use C libraries, but C programs are unable to use Go libraries.

Can I supply Go variables as parameters to C/C++ functions ?

Yes. The ability to pass Go values to C (cgo) is restricted by the Go type system. For example, the C type int is accessible from Go as C.int, so the programmer has to use an explicit type conversion even when passing integers (this verbosity is intentional).

Go source codes interfacing to C also make use of the unsafe types uintptr and unsafe.Pointer.

Are their pointers compatible ? Go runs on a native Vm, so I guessed there might be an abstraction layer over the memory that invalidates C/C++ pointers

Pointers are compatible.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote