18年5月份接触了51CTO推出的微职位Go课程,对Golang十分喜爱。通过张长志老师的视频讲解,前后4个月的学习时间。也用Golang写了些简单的代码和例子,其中包括业余时间的,也有项目中的。今天写点Golang相关的总结,仅供各位同学参考。
Fewer features and simple syntax. Go is advocating minimalism, advocating less is more. This is particularly evident in its spec, which can be read in an afternoon. Go has very few features and many go users have feedback that the Go keyword is at least completely recorded in the brain. At the same time its syntax is extremely simple and semantically clear.
Easy to deploy. Go is a strongly typed static language that compiles code into local machine instructions. Its runtime is linked to the execution file at compile time, which means we don't need to install a JVM like java. And the compiled execution file itself does not depend on other dynamic libraries, it can be easily released. Of course, if you use go to write code to invoke some of the dynamic library interfaces, you still need to deploy the dynamic library according to the actual situation. This is a favorite for many friends who go from Python/java to go.
There is a better standard library and more robust. The standard library of go itself is quite comprehensive, ranging from file archiving, compression, encryption, database to data serialization, character formatting, checksum and network libraries, synchronization libraries, and more. Basically can meet a lot of basic needs. Better yet, the quality of these standard libraries is very high and robust. The interface is also relatively simple and has a clear document description. Along with the development of the two years, Go's third-party library also more up, although probably not as much as Python, but the development time, it is very good.
Integrated Test frame. Writing unit tests is not an easy task when writing code in C + +. It takes some skill and effort to get it done. However, the unit Test framework is integrated in go, as long as the source file ends in _test.go, the unit test can be performed directly from Go test. It also provides a code test coverage tool that makes it easy to implement automated testing. In addition to this, the benchmark framework function is integrated, which makes it easy to measure the efficiency of the functions you write. In addition, there are performance profilers that can be used at run time to dissect the bottleneck points of the program, which can then be optimized.
Sound code style with check tool. When I first learned go, many articles and books would refer to the Go FMT command, unifying the code style. I think this is the solution to the style of the dispute. The effect is that others write the code feel is also written by themselves. And Golint, you can write code according to the style and requirements of Go team. And go vet can be used to check some of the pits that are hidden in go.
Simple but powerful package management. Package management for Go may seem too weak in many other languages, but in my opinion it solves two of the problems I need, one is the cyclic dependency problem, the go is a packet that refuses to have a cyclic dependency, and the other is the initialization of the package, and the file for each package can implement an init function to execute at import time. This is very useful when working together.
Easy to write cross-platform code. If you use pure go and don't involve CGO, you can easily cross-platform. Just before the file suffix. Go, introduce _linux,_windows,_x86,_x64 and other characters as the end of the filename prefix can be done only in the corresponding platform to compile. Go also has the build constraints control code under what conditions compile. If you use CGO, it involves a cross-platform problem with C, so it's a little cumbersome, but the problem is not too big.
Garbage collection. The existence of GC greatly reduces the writing of concurrent Code, and also provides the robustness of the program. As a programmer who has been a driver of development experience from C + +, GC is something I have never dabbled in. For me, the GC equals a nightmare. But when I started trying to accept the GC, I found that the GC really solved the programmer's productivity and greatly improved the efficiency. Although go is now 1.4 version, but GC is not good, according to go roadmap, there will be a better GC implementation added.
Interfaces and structs. When I first learned about the interface of go, my first reaction was that it was what I wanted. Although many people also say go of this interface of bad, and say very reasonable, such as Lao Zhao's "Why I do not like go Language interface". Interface can be extended to new interface,struct by combining them, or by combining them into new structs. No inheritance, only combinations. Similar inheritance can be achieved by an anonymous combination. Interface can be queried, somewhat like the taste of COM, but more simple at the syntactic level. A struct-to-interface mapping is implicit, and there is no need to declare a struct to implement a interface. Although there may be conflicting names, they can be resolved through wrapper. Another benefit of this interface is that mock-up is easy to implement in unit testing, which is a great favorite. You can also read this article "Go Interfaces make test stubbing easy"
A unified work layout. Go defines the directory structure of the project, such as the bin directory, the PKG directory, and the SRC directory. This is consistent with the layout of my daily projects. It was the same arrangement we had before in C + +, so for that, I felt that it was easy to transition.
Built-in concurrency primitives. Referring to go will have to mention Goroutine and channel. The cheap goroutine allows us to happily handle asynchronous tasks, and the channel can be used to exchange data. With Goroutine, it is easy to achieve high-performance service-side. Goroutine and its scheduler can be easily combined with EPOLL,IOCP and other system mechanisms, and then through the half sync/half async mode, it is easy to achieve synchronous form at the grammatical level without losing performance.
The Go Standard library also provides a sync package, with basic mutexes saying that there are read and write locks like Rmutex, and Once,waitergroup. Basically meet the daily demand for locks.
Go to help programmers solve the race condition problem frequently encountered in concurrency, also provided the corresponding race condition tool. There are also corresponding deadlock detection tools.
Although the go community has a slogan: "Do not communicate by sharing memory; Instead, share memory by communicating. ", but each goroutine is not completely independent, and can be shared with the data. This time can only rely on the programmer himself to abide by. And because Goroutine is not completely independent, panic this kind of thing can cause the whole program to hang out. This is not really good compared to Erlang.
The defer of a sore egg. Used to Raii C + +, it is very offensive to go defer mechanism, but sometimes have to use. The reason is that this defer is not a block level, but a function-level one that needs to be executed before the function returns. So this leads to a lot of pain when dealing with logic like file open, operation and shutdown, and back to the age of C, you have to go to close manually.
The panic of a sore egg. Although I do not use the exception under C + +, but I am very dissatisfied with the design of panic. Because it affects the global. To capture panic, you need to use defer. If panic just let the current goroutine hang out, I think it's a bad skin.
There is no generic type. Go does not have a generic type of egg pain place is, either use interface{} to do the run-time generics, or you can manually write code generator. For example, I'm going to have to make a generator in order to generate the network protocol serialization code. And because there is no generics, want to implement similar C + + STL container and algorithm is not very likely, of course, the method is still there, continue to use the code generator. and GO1.4 simply introduced a command called go generate.
Summarize
Some of the other built-in data structures in go, such as Slice,map, are also criticized because it does not give programmers the benefit of the range keyword.
Among all the features of go, the favorite is Gc,goroutine,channel and interface. And the rest of the features (like many of the features I've enumerated above) are not very important, and many of them can be practiced in the project, and the language itself is not too much of a relationship.
Summed up, this thing is an engineering tool, all kinds of easy to use, but from the design perspective of a variety of rough, there is no need to over-overestimate. It counts as a good friend in engineering practice. When writing a server, it is a sharp weapon, at least when writing a service-side program, I feel so.
A friend says a language is good to see whether it has opened up your horizons, give you new ideas, I think at least go on this satisfaction.