Who uses the cgo or c-package mechanism with their own compiler combination

I’m still a beginner programmer.
I think I’ll use a combination of my own compiler and golang.
So I would like to know how cgo works.
I know that cgo is hard to understand, but I would appreciate if you could give me a link or explanation of how the C package works.
I would really appreciate it if you could tell me (^ _ ^ )

If you haven’t done so already, I recommend reading the cgo command documentation:
https://golang.org/cmd/cgo/
And the Golang blog post:
https://blog.golang.org/cgo
However I would also recommend some of Dave Cheney’s posts on cgo:
https://dave.cheney.net/tag/cgo

1 Like

Yes, if you read carefully, my question is "Does using #include slow down the process? That part is not clarified in the article. I am (?) I am someone who is prone to…

Using cgo can slow down both compilation and execution. Compilation is slower because Go has to run a C/C++ compiler to compile the non-Go code in addition to the Go code and execution is slower because Go’s runtime and calling conventions are not compatible with C so there is overhead in making calls between Go and C/C++.

And by the way, thanks for letting me know about the question.
Just to confirm my understanding, I want you to check the similarities.
That is, using import "C" will run the C / C ++ compiler to compile the Go code. Therefore, it will take a long time to compile.

Only a Go compiler can compile Go code by definition (i.e. a C/C++ compiler that can also compile Go would be a Go compiler). import "C" is a “magic” import in Go and when the Go compiler sees it, the Go compiler will start the C compiler to compile the commented text above import "C". I don’t know the exact way the Go compiler provides this commented text to the C compiler; it might be copied into a temporary file that’s passed as input to the C compiler. After the C compiler compiles the C code, the Go compiler then needs to generate wrapper code around anywhere any of the C functions are called because C’s calling conventions and usage of OS threads is different and incompatible with how the Go runtime executes Go functions and goroutines. Then the compiled Go and C code have to be linked. Based on a few Google searches, one of which took me here, it looks like linking might be a step where a lot of time is spent, but I do not know the specifics here.

The only place I have ever used cgo is here but I couldn’t tell you why I made this; I don’t remember. Whatever the reason was, I found out how to do it in Go without cgo and stopped using this.

1 Like

Thank you for telling me deeply about the magic import where my perception is ambiguous ^ _ ^

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.