#What language is Golang’s compiler written in?
It seems like a "chicken-and-egg" problem, but it's actually not. (Recommended study:go)
The key to this problem is to understand that the compiler itself is a program, and its function is to translate the source code into a computer executable program.
For local languages such as C/C, Go, and Rust, the target program they compile and generate is the local executable program of the target platform. For example, if you write Hello World in these three languages, the result of compilation under the Windows platform must be an .exe file. These .exe files do not contain any source language information.
Windows does not know when running them, nor does it need to know what language these .exe files were written in before compilation. It only needs to load them into memory and execute the instructions one by one.
The same is true for compiler programs.
If you invent a new language CNifeLang, you first need to write a CNifeLang compiler in an existing language, for example, use Golang;
You I wrote a CNifeLang compiler in Golang, which can successfully compile any legal CNifeLang source code into an equivalent target program.
Then use CNifeLang to write a program, which has the same function as the compiler you wrote in Golang. Use the compiler implemented in Golang to compile the program, and you will get a CNifeLang compiler implemented in CNifeLang. .
Because the function of this program is exactly the same as the compiler written in Golang, you can use this compiler to compile CNifeLang, so CNifeLang implements bootstrapping.
If you have studied the course on compilation principles, you will find that compilation has nothing to do with programming languages. It has a complete and mature set of operating methods. You can use any Turing-complete language to write a compiler for any formal language, and there are strict mathematical methods to prove that the compiler you write is correct.
Our common programming languages basically implement bootstrapping, which is one of the signs of the maturity of a programming language. Because the compiler is a complex project and has a mature evaluation system. Being able to write a mature, usable, and efficient compiler for this language means that the language is ready for use.
The above is the detailed content of What is the golang compiler written in?. For more information, please follow other related articles on the PHP Chinese website!