Do you know most of the codes of Go programming language, even its compiler is written with Go? There may be a question in the head after hearing this, "How is that possible?" You need another Go compiler to make a Go compiler, right? "
It's a classic "egg before or chicken before" kind of question. And the answer lies in a cool computer science concept called Bootstrapping.
What is the Bootstrapping thing?
In simple terms, bootstrapping is using a small or simple system to create a bigger and stronger system. Something like lifting yourself up by holding your shoe lace! This is exactly what happened to Go.
Let's see how it works step by step:
- The first compiler was written in another language: The first compiler of Go was not written in Go language. It was written using C language. This C-based compiler had only one job: compiling the Go Source Code into an executable program.
- Written a new compiler with Go: After that, Go developers wrote the source code for a whole new compiler using Go language.
- The real magic: Bootstrapping is done in this stage. The Go Team used their old compiler made with C to compile the source code of the new compiler written with Go. So they got a compiler (executable file) made of Go.
- Go Self-Sufficient: Once Go could build his own compiler, there was no need for the old C-based compiler. From now on Go starts building himself. Means, Go 1.4 is used to compile the Go 1.5 version. Used Go 1.5 to compile Go 1.6 again This is how the chain goes. So, next time you hear "Go is written in Go", don't be surprised! This bootstrapping method uses not only Go, but also large programming language like C, Rust, Java. This is the sign of maturity or maturity of a language. Some questions may arise in your mind as such:
_
- Q: What is the advantage or profit of this? Wouldn't the compiler made of C be faster? Answer: There are many benefits such as maintenance, contributor productivity, tooling consistency, portability, and feature development speed etc. will be easy. Many common bug of RC can be avoided.
- Q:At the end of the day, then, the base of the Go compiler is in C, right? Answer: A compiler written in C, called the C-Compiler, is used to build the first version of the Go compiler from its Go source code. This process creates an executable file: go_compiler_v1.exe. Next, go_compiler_v1.exe is used to compile a newer version of the Go source code. This creates go_compiler_v2.exe, which can then be used to compile the next version, and so on. Thus, the C-Compiler is no longer needed.
- Q: Isn't it the same for Java? Answer: Many important languages follow the bootstrapping technique, but by no means is it a universal rule for success.
Top comments (0)