preface

As Docker is widely used in the company, I suddenly got to know its programming language (Go). And I feel that the application of Go language is becoming more and more widespread, many Internet giants are using it. At present, I use my spare time to learn it, and the mainstream is Java. After learning it, I may consider turning to 🤡. If you are also interested in learning together.

Go to introduce

  • A brief history of

    Go is an open source statically compiled language released by Google in 2009. The Go language was first developed in 2007 by Robert Griesemer, co-developer of the Java HotSpot VIRTUAL machine and V8 engine, and Ken Thompson, a member of the Bell LABS UNIX team, Cofounder of C, UNIX, and Plan9) and Rob Pike in their spare time. To date, Go has developed its own language, and the community is thriving, including a large number of killer projects (Moby, Docker, Kubernetes, Gogs, Grafana, Etcd).

  • features

    1. The Go language has automatic garbage collection and allows developers to interfere with the collection.
    2. Go has richer built-in types and a more streamlined syntax for error handling.
    3. Go language functions support multiple return values, and functions are also types that can be passed as arguments.
    4. Go language is optimized for multi-core processor programming, which can realize concurrent programming from the aspects of program and structure.
    5. Go’s initial compilation speed is comparable to C/C++, and its secondary compilation speed is significantly faster than C/C++, while at the same time approaching the simplicity of interpreted languages such as Python.
  • Naming conventions

    The project name of Go language generally adopts [domain name + project name], for example: github.com/example. The directory named in this way can be divided into projects and is not easy to repeat names; it can quickly find the address of the original project; and it is convenient to obtain code packages.

  • Code force formatting

    After you save the code, you can use the Go FMT tool to format it. Of course, even if you do not manually format the code, the Go compiler will automatically format all the source code at compile time. This mandatory formatting can effectively unify the coding style.

The development environment

Go supports Linux, FreeBSD, Mac OS, and Windows. The installation package can be downloaded from golang.google.cn/dl/.

Basic commands and usage

  • After installing the Go language, you can use the Go command. Enter Go help to check which commands are available:

    $ go helpGo is a tool for managing the go language source code. Usage: gocommand[arguments] contains the following commands: -build: Compiles source code packages and dependencies. -clean: Deletes object files. -doc: displays documents of the Go package or program entity. - env: Displays variables of the Go environment. - bug: indicates the bug submitter. - fix: indicates the repair program. - FMT: formats the codes in the source package. - Generate: Identifies regular commands to run by scanning the Go :generate comment in the Go source code. - get: downloads and installs the specified packages and dependencies. - install: compiles and installs the specified packages and dependencies. -list: displays information about the specified source package. -run: Compiles and runs the Go program. -test: Tests a source package. -tool: Runs a specified tool. - version: Prints the Go environment version. -vet: checks the source code package for possible errors. use"go help [command]"View the details of each command.Copy the code
  • The directory structure

    1. GOROOT structure

    $GOROOT, as the root of the Go locale, places the following:

    & cd $GOROOT && ls -l
    -rw-r--r--    1 root  wheel  55389  9 26 02:52 AUTHORS    # List of all people involved in the development of Go
    -rw-r--r--    1 root  wheel   1339  9 26 02:52 CONTRIBUTING.md    Contribute code to the Go language
    -rw-r--r--    1 root  wheel  84339  9 26 02:52 CONTRIBUTORS   # List of all contributors
    -rw-r--r--    1 root  wheel   1479  9 26 02:52 LICENSE    Open source agreement for the # Go language
    -rw-r--r--    1 root  wheel   1303  9 26 02:52 PATENTS    # Go language patent description
    -rw-r--r--    1 root  wheel   1607  9 26 02:52 README.md    # specification file
    -rw-r--r--    1 root  wheel    397  9 26 02:52 SECURITY.md
    -rw-r--r--    1 root  wheel      8  9 26 02:52 VERSION    # Go language version file
    drwxr-xr-x   19 root  wheel    608  9 26 02:55 api  # Go API dependencies (variables, constants, functions)
    drwxr-xr-x    4 root  wheel    128  9 26 02:55 bin   Used to store standard command execution files, go, godoc, GOfmt three sets
    drwxr-xr-x   50 root  wheel   1600  9 26 02:55 doc   Store library documents
    -rw-r--r--    1 root  wheel   5686  9 26 02:52 favicon.ico  # Go language icon
    drwxr-xr-x    3 root  wheel     96  9 26 02:55 lib   Store some special library files
    drwxr-xr-x   16 root  wheel    512  9 26 02:55 misc   Accessibility tools and instructions
    drwxr-xr-x    6 root  wheel    192  9 26 02:55 pkg    Save the files generated by the Go language standard library
    -rw-r--r--    1 root  wheel     26  9 26 02:52 robots.txt    # Disallow search engines from indexing locally launched Go documents
    drwxr-xr-x   71 root  wheel   2272  9 26 02:55 src    # Store GO language own source code
    drwxr-xr-x  327 root  wheel  10464  9 26 02:55 test   Store test validation related files
    Copy the code
    1. GOPATH structure

    $GOPATH is the working directory where we develop the GO language. This directory usually has three folders:

    -bin: saves the executable files generated by Go install$GOPATHThe /bin PATH allows you to use the executable files in this folder anywhere in the world by adding the PATH environment variable. - PKG: stores files generated by go editing. -src: Store the source code of the Go project we developed, not separated by the package name of the project code.Copy the code

The development tools

  • Visual Studio Code

    Visual Studio Code, an open source editor from Microsoft, is one of the best tools for developing Go programs. The vast ecosystem makes it more than just an editor, it also has most of the toolchains used in Go development.

  • VIM-Go

    The VIM editor is one of the favorites of most programmers. There is also a popular GO language development plug-in —- vim-Go in the VIM ecosystem. Installing this plug-in is the same as installing other VIM plug-ins.

  • Gogland

    IDE (Goland) from the well-known Jetbrains company is an IDE designed specifically for Go language development.

  • LiteIDE

    LiteIDE is a cross-platform lightweight integrated development environment developed specifically for the GO language. LiteIDE is an open source tool with code hosted on Github.

The first Go program

Now began to write the first program, create a directory, such as code, local, and then set up a project called hello – word, eventually directory PATH for the $PATH/SRC/code. The local/hello – word