** Question: ** The same Hello World program, why go compiler size is much larger than C compiler
Because C is compiled in the form of dynamically linked library, while GO is compiled in the form of static compilation, resulting in large volume of compiled binary files.
The idea of dynamic loading was introduced in the early computer memory resource which was very valuable, and the dynamic link library was produced.
Today’s computers use tens of gigabytes of memory, so there is no need to save hundreds of gigabytes or 1M of memory.
Go uses static compilation, does not rely on any dynamic link library, can avoid various dynamic link library dependency problems;
Once compiled, as long as the platform is consistent (Linux AMD64), it can be deployed arbitrarily, regardless of linked library dependencies;
This is a good thing, someone joked that the compiled executable is too big?
Dynamically linked library
In the use of dynamic link libraries are not all the dynamic library code copy to the generated program, but when the program is run again to load the corresponding code, so programs that use the same dynamic libraries can share a code, and when you have to upgrade the dynamic library, also need not to modify the code that USES it.
At the same time, the problem of storing multiple copies of the same machine code in physical memory is solved.
Which process needs to be able to map this code to its own virtual address space;
And you can update dynamically linked library content at any time without stopping already running programs;
That is, when the program runs, it takes up a certain amount of memory, and when the dynamic link library is loaded, it is loaded into the memory. The program simply maps the address of the dynamically linked library to its own memory space for the call;
Dynamic link library loading
- Implicit linking: requires.lib.h.dll, which loads the file into memory when the program starts executing. #pragma comment(lib, “Dll.lib”)
- Explicit linking: requiring.h. DLL, loading in real time, loading only when the program needs it;
- So the Go plugin is implicitly linked;
How to upgrade a running DLL
Open a thread detection DLL, when found a new version, uninstall the old DLL, and then load the new;
How does Go upgrade plug-ins?
p, err := plugin.Open("plugin1.so")
p, err := plugin.Open("plugin2.so"When the second "so" is loaded, the data in the previous "so" is not accessible. This means that the data in the previous "so" cannot be hot updated.// The first plugin still takes up memory. Waiting to be GC
// The go plugin only opens the module and extracts the symbol, but does not close it.The same so file can only be opened once for hot update only to update functions, not data/ / compile
go build -ldflags "-pluginpath=plugin1" -buildmode=plugin -o plugin1.so *.go-- ldFlags -pluginPath is used to identify internal paths that are different for each build, avoiding warnings about reloadingCopy the code
The dynamically linked library under Linux is.so, and the static library is.a
Windows dynamic link library is. DLL
It should be noted that the go plugin only supports Linux and is not available under Windows.