[Translate] Go Language Overview

Go Language Overview

This article Abstract: This paper summarizes the definition of very general language of Go, ecosystems and implementation, but also try to give a reference document with different needs corresponding details, see the end of this article.

Whenever we talk about "Go language", it may be because of different scenarios talked to a lot of different things. So I try to do an overview of the Go language and its ecosystem, and related documents are listed in each section of the book (which may be a bit like a hodgepodge, which also contains many practical problems I recently encountered). let's start:

Go Programming Language

Go language is a programming language. As an authority, Go language specification defines the meaning of the format specification and code code represents. It does not meet the specifications are not Go language. Similarly, the specification is not what has been mentioned is not considered part of the language. The specification is currently maintained by the development team of Go, a new release every six months. I write this article the latest version 1.12.

Go Language Specification specifies:

  • grammar
  • Type variable value, and Semantic
  • Pre-declared identifiers and their meanings
  • Go Run program
  • Special unsafe packets (although not contain all of the semantics)

The specification should be enough to make you realize a Go compiler of the language. In fact, there are already a lot of people on this to achieve a number of different compilers.

Go compiler and run-time

The language specification is only a text document, which is itself less useful. What you need it is to achieve these semantic software that compiler (analysis, examine the source code and converts it into an executable form) and runtime (the time required to provide runtime environment code). There are many such software portfolio, they are more or less a little different. Examples are as follows:

  • gc, Go language development teams to develop their own language to achieve pure Go (there is a small part of the compilation includes) the compiler and runtime. It is released together with the Go language. And other such tools is different, gcit is not strictly distinguish between the compiler, assembler and linker - they share a lot of code in the realization of, and share or pass some important responsibilities. Therefore, generally can not be linked by different versions of the gccompilation of the package.
  • gccgo and libgo , and its front end gcc runtime. It is implemented in C, and is also maintained by Go development team. However, because it is part of the gcc organization, and in accordance with the release of gcc release cycles, it is often slightly behind the Go language specification of the "latest" version.
  • llgo , the LLVM front end. I do not quite understand it.
  • gopherjs , the Go code is compiled into JavaScript, and uses a JavaScript VM and some custom code as a run. In the long run, due to the gcgained native support WebAssembly, and it is likely to be eliminated.
  • tinygo , realized for small-scale programming is incomplete. It can be customized run is run on a microcontroller (bare metal) or WebAssembly virtual machine. Because of its limitations, technically all the features it does not achieve the Go language - it is mainly reflected in the lack of garbage collection, concurrency and reflection.

There are many more to achieve, but it has been enough to make you understand the different implementations. Each of the above methods are used in different ways to achieve the Go language and have their own distinctive characteristics. The difference they may exist there (To illustrate this point, some of the following statement may be a bit strange):

  • int/ uintSize - length may be 32-bit or 64-bit.
  • Implementation of the basic functions of running, such as memory allocation, garbage collection and achieve concurrent.
  • Traversal maporder is not defined in the Go language - gcapparently this type of operation will be randomized, and gopherjswill use the JavaScript you are using traversal.
  • appendOperating allocated additional memory space required - however, in the allocation of additional space , do not allocate more memory space again.
  • unsafe.PointerAnd uintptrconversion mode between. In particular, gcto take effect when the conversion should have its own rules . Typically, unsafethe package is virtual, it will be implemented in the compiler.

In general, according to some specification details are not mentioned in (especially those details mentioned above) can make your program can use a different compiler to compile , but often the program will not like you expected to work . Therefore, you should try to avoid that from happening.

If your language is Go through "normal" channels to install it (download and install the official website or through the package manager to install), then you will get the development team of Go gcand formal operation. In this article, when we discuss "Go is how to do", if not otherwise specified in the context, that is, we usually talk about gc. Because it is the most important realization.

Standard Library

The standard library is included with the Go language of a group of dependencies, which can be used to build a number of practical applications immediately. It also maintained by Go development team, and will be released along with the Go language and compiler. In general, a certain standard library can only rely on its issued jointly by the compiler to normal use. Because most (but not all) are part of the standard library runtime (included in the main runtime, reflect, syscallpackage). Since the compiler needs to be compatible to run currently in use at compile time, so their version to be the same. Standard library API is stable and will not change in an incompatible way, so Go program based on a specific version of the standard library can also be written in normal operation in a future version of the compiler.

Some standard library will fully realize their entire library of all content, while others achieve only part - developers in particular will runtime, reflect, unsafeand syscallpackages to implement custom features. For example, I believe AppEngine standard library is re-implement some of the features of the standard library for security reasons. Some of these reimplemented usually try to keep transparent to the user.

There is also a separate library other than the standard library , which is layman's terms xor "extensions." This library contains some code Go development team simultaneously developed and maintained, but will not have the same release cycle and Go language, and compared to the Go language itself , compatibility will be poor (functional and maintenance will poor). Where the code is either experimental (in the future may be included in the standard library), or is compared to the standard library function is still not generic, or in some rare cases, developers have provided a synchronization code can review and Go development team.

Again, if no additional pointed out, referring to the "Standard Library", we mean the official maintenance and released, hosted golang.org on the Go standard library.

Code Morphing Tool

We need to build tools to make the code Go language is easy to use. The main responsibility is to build tools to find the need to compile the package and all dependencies and call the compiler and linker according to the required parameters. Go language has support for the package , at compile time allows multiple source code files as a unit. This embodiment also defines the introduction and use of other packages. But it is important, it does not define a path mapping by importing the package source files, nor does it define the package import location on disk. Thus, each building tools for this problem has a different approach. You can use common building tools (such as the Make command), but there are many building tools specifically for the Go language students:

  • Go Language Tools [1] is to build tools Go team official maintained. It and Go language ( gcand standard libraries) have the same release cycle. It requires a named GOROOTdirectory (this value is taken from the environment variable, it will have a default value at installation) to store the compilers, libraries, and other standard tools. It requires that all of the source code to be stored in a named GOPATHdirectory (this value is also acquired from the environment variable, by default $HOME/goor a value equivalent thereto). For example, the packet a/b's source code should be located as $GOPATH/src/a/b/c.gothe path. And $GOPATH/src/a/bthe path should only contain the source files in a package. In the distributed mode, a mechanism can be recursively downloaded from a package and its dependencies on any server , even if this mechanism does not support the version control or download calibration. Go language tool also contains a number of other toolkits, including Tools, Go read the documentation of test code (for golang.org with Go language tools deployed), submit bug tools and a variety of other gadgets.
  • gopherjs own build tool, which mimics the Go language tools to a large extent.
  • gomobile is a purpose-built tool Go code for the mobile operating system.
  • DEP , gb , Glide , and so is the build and dependency management tool for community development, each of which has its own unique file layout (some can be compatible with the Go language tools, and some are not compatible) and dependencies declaratively.
  • bazel is an internal Google tool to build open-source version. Although its use is not limiting Go language, but the reason I put it as a separate item, because the language is often said Go tool is designed for the Google service, and conflict with the needs of the community. However, Go language tools (and many other open tools) can not be used by Google, because bazel use incompatible file layout.

Code Morphing tool is an important tool for most users to use when writing code directly, so it largely determines the Go language ecosystem aspects, but also determines the combination package, which will also affect between programmers Go communication and communication. As mentioned above, Go language tools are implicitly referenced (unless specified a different operating environment), thus significantly affect its design will make public perception of "Go language". While there are many alternative tools available, these tools have also been widely used as internal use and other scenarios, but the open source community generally want to use Go Go language tools and languages fit, which means:

  • You can get the source code. Go language tool for binary package distribution only extremely limited support, and the only support will be removed in a future release.
  • It should be based on Go official document formatting to write the document.
  • To include test cases , and can go testrun the tests.
  • Can completely through go buildto compile (described later with features collectively referred to as "Go may be obtained by" - "go-gettable") . In particular, if the source code needed to generate or meta-programming, is used go generate and submit the generated member.
  • Namespace path by introducing a first portion of a domain name, the domain managed code may be a server or a service running on the Web server, the source code can be found Go and its dependent and may work .
  • Each directory has only one package, and may use the code constraints build conditional compilation.

Go language documentation tool is very comprehensive, it is a good starting point to learn how to Go various ecosystems.

Other tools

Go language standard library contains some of the source code can interact with the Go package and contains more features x / tools extensions . Go language and therefore has a very strong third-party tools to develop culture in the community (because the official strongly want to stay lean Go language itself). These tools often need to know the location of the sources, you may also need to get the type information. go / build package followed the convention Go language tools, so it can be used as the document itself part of its build process. The disadvantage is that it's built on top of the tool is sometimes not compatible with other code-based build tool. So there is a new package being developed , it can be well integrated with other build tools.

Go language actually has a lot of tools, and everyone has their own preferences. But roughly as follows:

to sum up

I want to use a short list of references to the end of this article, the content of the list is prepared for those who feel confused beginners. Please click on the link below:

In addition there are many valuable documents can complement, but these should be enough to make you have a good start up. Go language as a beginner, if you find this article have any omissions (I might add more details) or you find any valuable information, please contact me via Twitter . If you are an experienced developer in Go, and you find that I missed something important (but I'm willing to overlook some important reference material, so that beginners can feel the freshness in the Go language learning : smile :), please give me a message.


[1]Note: Go development team is currently working on modules do some support module is above the package code distribution unit, which includes support for version control and can make some of the "traditional" Go language tool to solve the problem of foundation work. After the completion of these and other support, all the contents of this paragraph are essentially obsolete. Support for modules at present is there, but not yet part of the Go language. As the core content of this article is a different part of the Go language is a brief introduction, the content is not easy to change, now I believe that understanding these historical issues are also necessary.

If you find there is a translation error or other areas for improvement, welcome to Denver translation program to be modified and translations PR, also obtained the corresponding bonus points. The beginning of the article Permalink article is the MarkDown the links in this article on GitHub.


Nuggets Translation Project is a high-quality translation of technical articles Internet community, Source for the Nuggets English Share article on. Content covering Android , iOS , front-end , back-end , block chain , product , design , artificial intelligence field, etc., you want to see more high-quality translations, please continue to focus Nuggets translation program , the official micro-blog , we know almost columns .

Guess you like

Origin juejin.im/post/5d386166e51d454fd8057c6a
Recommended