Ethereum go code compilation
a total of 22 weeks, divided into five stages,
the first stage is 4 weeks, go language foundation and network concurrency, learning the introctory go language,
the second stage is 4 weeks, go language practical web development, crawler development, cryptography, consensus algorithm, realizing lightweight public chain, learning the website and crawler that can develop golang, Achieve lightweight blockchain
stage 3, 4-week Ethereum source code analysis and smart contract DAPP development, master Ethereum core and smart contract development, as well as blockchain,
stage 4, 4-week super ledger, bitcoin EOS, source code analysis and smart contract practice, master super ledger development, cat bitcoin, bifurcated EOS after learning, As well as DAPP development of smart contract
phase 5, 6-week project practice, five enterprise level projects, and one year of blockchain project experience after learning
from the characteristics of the language itself, go is a very efficient language, which highly supports concurrency. Go language itself pays more attention to distributed system, and concurrency processing is relatively good, such as advertising and search, That kind of high concurrency server
go language advantages:
excellent performance, can be directly compiled into machine code, does not rely on other libraries, go is extremely fast. Its performance is similar to Java or C + +
concurrency is supported at the language level, which is the biggest feature of go. It is born to support concurrency, and go is the concurrency supported in gene, which can make full use of multi-core and make it easy to use concurrency
the built-in runtime supports garbage collection, which is one of the features of dynamic language. Although GC is not perfect at present, it is enough to cope with most of the situations we can encounter, especially GC after go1.1
it's easy to learn. The authors of go language all have the gene of C, so go naturally has the gene of C. There are 25 go keywords, but the expressive ability is very strong. It almost supports most of the features you've seen in other languages: inheritance, overloading, object, etc
rich standard libraries, go has built a large number of libraries, especially the network library, which is also my favorite part< As like as two peas, br / > built-in powerful tools, Go language is built with many tools chain, the best should be gofmt tools, automatic formatting code, make team review become so simple, code format is exactly the same, it is very difficult to think differently.
cross platform compilation and fast compilation. Compared with the sluggish compilation speed of Java and C + +, the fast compilation time of go is a major efficiency advantage
disadvantages of go language:
package management: package management of go language is absolutely not perfect. By default, it has no way to make a specific version of the dependency library, nor can it create replicable builds. In contrast, python, node and Ruby all have better package management systems. However, with the right tools, the package management of go language can also perform well
lack of development framework: go language does not have a major framework, such as Ruby's rails framework, Python's Django framework or PHP's laravel. This is a heated discussion in the go language community, because many people think that we should not start with using frameworks. This is true in many cases, but if you just want to build a simple crud API, it's much easier to use Django / djrf, rails laravel, or Phoenix
exception handling: go language can help developers deal with compilation errors by simply returning errors (or call stack) through functions and expected calling codes. Although this method is effective, it is easy to lose the scope of the error, so it is difficult to provide meaningful error information to users. Error package can solve this problem by allowing us to add context and stack trace to return error
another problem is that we may forget to handle errors. Static analysis tools such as errcheck and megacheck can avoid these errors. Although these solutions are very effective, they may not be the right way.
public static String reverse1(String str)
{ return new StringBuffer(str).reverse().toString();
}
2.最常用的方法:
public static String reverse3(String s)
{ char[] array = s.toCharArray();
String reverse = ""; //注意这是空串,不是null
for (int i = array.length - 1; i >= 0; i--)
reverse += array[i];
return reverse;
}
3.常用方法的变形:
public static String reverse2(String s)
{ int length = s.length();
String reverse = ""; //注意这是空串,不是null
for (int i = 0; i < length; i++)
reverse = s.charAt(i) + reverse;//在字符串前面连接, 而非常见的后面
return reverse;
}
% CD $root / SRC
%. / all. Bash
the first step is a bit abrupt, because all. Bash only calls the other two shell scripts; Make. Bash and run. Bash. If you are using windows or plan 9, the process is the same, except that the script extension becomes. Bat or. RC. For other scripts in this article, please make appropriate changes according to your system
Step 2: make. Bash
. / make. Bash -- no banner
main. Bash comes from all. Bash, so calling exit will terminate the cheap process correctly. Main. Bash has three main tasks. The first one is to verify whether the environment for compiling go is complete. Integrity checking has been established in the past few years, and it usually attempts to avoid compiling with known broken tools or environments that are bound to fail
Step 3. CMD / dist
GCC - O2 - wall - werror - ggdb - O CMD / dist / dist - ICMD / dist CMD / dist / *. C
once the usability check is completed, make.bash will compile to generate CMD / dist, which replaces makefile compilation system before go 1. CMD / dist is used to manage a small amount of PKG / runtime code generation. CMD / dist is a program written in C language, which can make full use of the system c compiler and header file to deal with the detection of most host system platforms. CMD / dist is usually used to detect the host's operating system and architecture, that is, the environment variables $gohostos and $gohostarch. If it is cross compiled, the variables $goos and $Gorch may be different e to your settings. In fact, go is usually used as a cross platform compiler, but in most cases, the host and target systems are consistent. Next, make.bash calls the support library, lib9, libbio and libmach of the boot parameters of CMD / dist, uses the compiler suite, and then compiles with its own compiler. These tools are also written in C language, but compiled by the system c compiler< br />
echo "# Building compilers and Go bootstrap tool for host, $GOHOSTOS/$GOHOSTARCH."< br />buildall="- a"< br />if [ "$ 1" = & quot;-- no-clean" ]; then
buildall="& quot;< br />fi
./cmd/dist/dist bootstrap $buildall -v # builds go_ Bootstrap
uses the compiler suite CMD / dist to compile and proce a version of gotool, go_ bootstrap But go_ Bootstrap is not a complete gotool. For example, PKG / net is isolated, avoiding dependence on CGO. The list of files to be compiled and their dependencies are compiled by CMD / dist, so be careful not to introce new build dependencies to CMD / go
Step 4: go_ Bootstrap
now, go_ The last part of make. Bash is to use go_ Bootstrap completes the compilation of go standard library, including the replacement version of gotool< br />
echo "# Building packages and commands for $GOOS/$GOARCH."< br />"$ GOTOOLDIR"/ go_ bootstrap install -gcflags "$ GO_ GCFLAGS" 92;< br /> -ldflags "$ GO_ LDFLAGS" - V STD
Step 5: run. Bash
now, make. Bash is complete, running back to all. Bash, which will reference run. Bash. The job of run. Bash is to compile and test the standard library, runtime and language test suite
bash run.bash -- no rebuild
use the -- no rebuild flag because both make.bash and run.bash may call go install - a STD, which can avoid repetition, - no rebuild skips the second go install< br />
# allow all.bash to avoid double-build of everything
rebuild=true
if [ "$ 1" = & quot;-- no-rebuild" ]; then
shift
else
echo '# Building packages and commands.'
time go install - A - V STD
echo
fi
Step 6: go test - a STD
echo# Testing packages.'< br />time go test std -short -timeout=$(expr 120 \* $ timeout_ The next step of run.bash Z is to unit test all packages in the standard library, which is written with testing package. Since the code in $gopath and $root exist in the same namespace, we can't use go test, which may test all packages in $gopath, so we will create an alias STD to identify packages in the standard library. Because some tests take a long time or consume a lot of memory, they will be filtered by the - short flag< The next section of run.bash will run a large number of platform tests supporting CGO, run some spring tests, and compile some miscellaneous programs attached to go. Over time, this list of miscellaneous programs has grown, and the silence will inevitably be broken when they find themselves not included in the compilation process
Step 8: go run test
(XCD.. / test
unset gomaxprocs
time go run. Go
) | exit $
the penultimate step of run.bash calls the compiler and run-time test in the test folder of the $root directory. There are low-level tests that describe the compiler and the runtime itself. The tests in the subdirectories test / bugs and test / fixedbugs specially test the known and solved problems. The test driver for all tests is $root / test / run. Go, which is a small program that calls every. Go file in the test folder. Some. Go files describe the expected results on the first line, such as program failure or releasing a specific output queue< Step 9: go tool API# Checking API compatibility.'< br />go tool api -c $GOROOT/api/go1.txt,$GOROOT/api/go1.1.txt \
- next $go root / API / next.txt - except $go root / API / except. TXT
the last part of run.bash calls the API tool, which is used to execute the go 1 convention; The exported symbols, constants, functions, variables, types and methods constitute the go 1 API confirmed in 2012. Go 1 is written in the API / go1.txt file, and go 1.1 is written in the API / go1.1.txt file. Another additional file, API / next. TXT, describes the symbols that G 1.1 has added to the standard library and runtime since. When go 1.2 is released, this file will become the Convention of go 1.2, and another new next.txt file will be created. There is also a small file, except. TXT, which includes the approved extensions in the go 1 convention. The addition of documents is always careful.
Is it the go language
There are nine steps in thego compilation process
the first step. All. Bash
< pre t = "code" L = "Scala" >% CD $root / SRC%. / all. Bash < / pre > < P > the first step all. Bash just calls two other shell scripts: make. Bash and run. Bash. If you use windows or plan 9, the process is basically similar, except that the script ends with. Bat or. RC respectively. In the rest of the article, complete the command with the appropriate operating system extension
Step 2. Make.bash
< pre t = "code" L = "PL" > / / make.bash -- no banner < / pre >make.bash as a part of all.bash, If it exits, it will also interrupt the build process
Step 3. CMD / dist
< pre t = "code" L = "Python" > gcc-o2-wall-werror-ggdb-ocmd / dist / dist ICMD / distcmd / dist / *. C < / pre >when the sanity check is complete, make.bash starts compiling CMD / dist
Step 4_ Bootstrap
now go_ Bootstrap has been built, and the last step of make. Bash is to use go_ Bootstrap compiles a complete go standard library, including a complete go tool to replace it
echo"#$ GOOS/$GOARCH."< br />"$ GOTOOLDIR"/ go_ bootstrapinstall-gcflags"$ GO_ GCFLAGS"< br />-ldflags"$ GO_ LDFLAGS"- Vstd < / pre >
Step 5. Run. Bash
now make. Bash has been completed. Return to the execution of all. Bash, which will call run. Bash. The task of run. Bash is to compile and test the standard library, runtime and language test set
< pre t = "code" L = "Python" > bashrun.bash -- no rebuild < / pre >since both make.bash and run.bash call go install - a STD, you need to use the – no rebuild flag to avoid repeating the previous steps, and – no rebuild skips the second go install
#allowall.bashtoavoiddouble-buildofeverythingrebuild=trueif["$ 1"=& quot;-- no-rebuild"]; thenshiftelseecho'# Buildingpackagesandcommands.' Time go install-a-vstdechofi < / pre >Step 6. Go test - a STD
< pre t = "code" L = "PHP" > echo & ## Testingpackages.'< br />timegoteststd-short-timeout=$(expr120*$timeout_ Next, run.bash will run the unit tests written with the testing package on all the packages in the standard library. Because $gopath and $root have the same namespace, we can't use go test directly... Otherwise, every package in $gopath will be tested one by one. Therefore, we create an alias for the package in the standard library: STD. Because some tests take a long time and consume a lot of memory, the - short flag is used to filter some tests
Step 7. Runtime and CGO testing
run.bash the next part will run the platform's CGO support tests, perform some performance tests, and compile some miscellaneous programs that accompany the go release. As time goes by, the list of these miscellaneous programs will get longer and longer, so they will inevitably be separated from the compilation process
Step 8. Go run test
< pre t = "code" L = "Ruby" > (XCD / test
unsetgomaxprocs
timegorunrun. Go
) | exit $ The penultimate step of pre >run.bash calls the compiler and runtime tests in the test directory under $root. They're testing the compiler and the runtime itself for lower level details. It will execute language specification tests. The test / bugs and test / fixedbugs subdirectories store independent tests that have been found and fixed. The test driver is a small go program $goroot / test / run. Go, which will execute every. Go file in the test directory. The first line of some. Go files contains instructions that guide run. Go to make decisions about the results. For example, the program will fail or provide a certain output queue
Step 9. Go tool API
< pre t = "code" L = "Ruby" > echo & # 39# CheckingAPIcompatibility.'
gotoolapi-c $root / API / go1.txt, $root / API / go1.1.txt
- next $root / API / next.txt-except $root / API / except. TXT < / pre >the last step of run.bash calls the API tool
