Slashes and Dots in Function Names and Prototypes in Go's C Source
Go's C source code, as exemplified by the race.c file, incorporates non-standard character usage that raises questions about its validity.
Extension to C Standard
The C compiler used for Go, rooted in the Plan 9 C compiler, extends the standard with support for UTF-8 characters in identifiers. One notable extension is the special handling of the middot character (·).
Middot and Namespace Separation
The middot serves as a namespace separator in Go's internal linking process. When compiling a C file with UTF-8 identifiers, the middot is translated to a dot (.) in object files. The Go linker interprets this dot as a namespace separator.
Example
Consider the following C code written in UTF-8 without BOM:
Compiling this code produces the following symbols:
Notice that ""Bar1() has a capital B to make it visible to Go code. It is equivalent to the Go function "func Bar1() {}" in terms of the resulting symbol.
The "Empty" Namespace
The "" namespace is a special namespace used as a placeholder. When importing a package using this namespace, as in:
The linker replaces "" with the actual package path during importing. Thus, in the linked binary, we will find the following symbol:
Conclusion
The slashes and middots in Go's C source extend the standard, providing a way to namespace and differentiate function names. This convention facilitates building and linking Go applications.
The above is the detailed content of How do slashes and middots in Go's C source code contribute to function namespacing and linking?. For more information, please follow other related articles on the PHP Chinese website!