Go study notes: acquaintance Go Language

Go Language Introduction

  Go languages ​​is Google (Google) developed a static model, compiled and comes with garbage collection and concurrent programming languages.

  Go language style is similar to the C language. Syntax in the C language based on a substantial simplification, remove unwanted expression in parentheses, the cycle only for a representation method, traversal can be achieved various values, key value and the like.

  Go language most distinctive characteristic than goroutine. Go language in the language goroutine layer can achieve concurrent execution of the function by. goroutine similar thread but not a thread, gouroutine automatically scheduled in the Go language runtime. Therefore, Go written language is well suited for highly concurrent network services.

Go properties of language

  1. Compiled executable file output

  Go language code can be output directly to the target platform's native executable file. Go language does not use a virtual machine, only garbage collection (GC) and goroutine scheduling at runtime (runtime).

  Go linker to use their own language, it does not rely on any operating system, compiler, linker. Therefore compiled executable files can be run directly on virtually all operating systems and environments.

  After release from Go1.5, Go language bootstrap realize the function uses language Go Go language compiler and all the tools chain.

  Sample code, Go-language version of Hello world!  

//helloworld.go
package main

import "fmt"

func main(){
	fmt.Println("Hello,World!")
}

Compiled executable file commands: go build ./helloworld.go
After compilation, generate helloworld executable files in the current directory.
  Go language can not only output executable file, the compiler can also be exported C language library of static and dynamic library. At the same time, from the beginning Go1.7 version, Go-language support compile the code for the plug. Use plug-ins can be dynamically loaded module needs, rather than all at one time the code is compiled into an executable file.

  2. Simple engineering structures

  Go language source code without header files, compiled files from the source file suffix called go of; Go language without solutions, project files and Makefile. As long as the project file is filled in accordance with the rules of GOPATH, you can use go build

Or go install compile, build and install the binary executable file on a unified bin directory.

  3. Compile speed

  Go language can use their own characteristics concurrency compilation, the compiler is the minimum unit of concurrent package (package). From Go1.9 release, reduced to a minimum coding unit concurrent function, the overall speed compilation provides 20%.

  In addition, Go language syntax is simple, with strict engineering design, no header files, such as cross-dependencies package is not permitted rules in large part to speed up the process of compilation.

  4. High Performance

  Go language performance is close to the Java language.

  The native support for concurrency

  Go characteristics that support concurrent language from the native language level, without third-party libraries, Go language can easily help developers decide how to use the CPU resources in the Go language runtime.

  Go concurrent language is based on goroutine, goroutine similar threads, but not the thread. goroutine can be understood as a kind of virtual threads. Go language runtime scheduling goroutine be involved, and goroutine reasonably allocated to each CPU, maximizing the use of CPU performance.

  In plurality goroutine, Go language channels (channel) for communication, the program can be designed to require concurrent program producer and consumer model, into the data channel. The other end of the channel code data will be concurrently calculated and returns the results, as shown below.

Example, producers, consumers concurrent processing. A producer generating a second string, and passed through the passage to the consumer, the producer goroutine run concurrently using two consumers () function for processing goroutine the main.

/ * 
Program Description: Manufacturer generating a second string, and passed through the passage to the consumer, the producer goroutine run concurrently using two consumers () function for processing goroutine the main. 
* / 

Package main 

Import ( 
    "FMT" 
    "Math / RAND" 
    "Time" 
) 

// data producer 
FUNC Producer (String header, Chan Channel <- String) { 
    // infinite loop, kept production data 
    for { 
    // use rand.Int31 () generates a random number, using fmt.Sprintf () function formatted as a header and a random number string channel <- fmt.Sprintf ( "% s :% v", header, rand.Int31 () ) // wait 1 second time.sleep (time.Second) } } // data consumer FUNC consumer (Channel <-chan String) { // constantly obtain data for { // retrieve data from the channel, where It will be blocked until the return channel data Message: = < fmt.Println (Message) } } FUNC main () { // create a string type of channel Channel: = the make (Chan String) // create producer concurrent function goroutines Go producer ( "CAT", Channel) Go producer ( "Dog", Channel) // data consumption function consumer (Channel) }

6. Powerful standard library

  Go language standard library covering all aspects of the network, system, encryption, coding, graphics and so on. For example, you can directly use the http packet reception processing HTTP protocol; library of high-performance network operating system communication model (Linux's epoll, Windows of IOCP) based on; all encryption, decryption are built-in support, from third-party libraries do not need Obtain. Go language compiler is also part of the standard library, through the lexical scan source code, using the syntax tree branches and other logic to get the source code. Go surrounding language tool is built on these standards libraries. In the standard library almost complete most of the demand.

  Go language standard library package is provided by way of support, the following table is Go standard library of common language packages and their functions.                                       

Go language and its standard library functions commonly used package
Go language standard library package name Features
bufio Buffered I / O
bytes Operation of Byte
container The pile of packages, lists, and other lists annular container
crypto Encryption Algorithm
database And database-driven interfaces
debug Access to a variety of file formats commissioning and commissioning functions
encoding Common algorithms such as JSON, XML, Base64, etc.
flag Command line parsing
fmt The format operation
go Go language lexical, syntax tree types. And extracting the code information can be modified through the packet
html HTML template system and escape
image Access and generate common graphics formats
I Implement I / O interfaces, and access the original access package
math Math Library
net Network library that supports Socket, HTTP, Email, RPC and SMTP
the Platform-independent operating system platform dependent package
path Compatible path operations for each operating system utility function
plugin Go1.7 added plug-in system. Support compile the code for the plug, loaded on demand
reflect Language reflecting support. Value can be dynamically obtained type information code, retrieve and modify variables
regexp Regular Expressions package
runtime Runtime Interface
sort Sort Interface
strings String conversion, and practical analytical function
time Time Interface
text Text templates and Token lexer

 

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

7. coding style is clear, simple

  Go language syntax style is similar to the C language, but its essence on the basis of the C language, to its dregs, the C language more prone to error wording has been adjusted accordingly compiled tips.

(1) removing the cyclic redundancy brackets

// C language for loop 
for (int I = 0; I <10; I ++) { 
    // cycle Code 
} 

// Go language for loop 
for I: = 0; I <10; I ++ { 
    // loop code 
} 

  However, in Go, for both sides of the brackets removed, int variable type declaration is simplified as ": =", at compile time, the compiler can be directly through the ": =" R-values ​​derived type variable i.

(2) removing the redundant expression brackets

  The same simplification may also be present in the sentence is determined, the following example is a comparison and judgment statement of the C language in Go:

// C language statements to determine 
if (expression) { 
    // expression is established 
} 

// Go judgment statement language 
if expression { 
    // expression is established 
}

(3) mandatory code style

  Go language, left parenthesis "(" must be followed by other statements not wrap style brackets will be regarded as an example of an error code is compiled as follows:

// correct code style 
for I: = 0; I <10; I ++ { 
    // cycle Code 
} 

// Style error code 
for I: = 0; I <10; I ++ 
{ 
    // loop code 
} 

  The first two kinds of error coding style, will be reported at compile time compilation errors. Go mandatory language of this code style restrictions on the language level, the time may make the beginning of the developers will be some not used. Meanwhile, Go language also provides a set of formatting tools. Some Go language development environment or editor when you save the code will be formatted using the formatting tools to modify the code, so that when the code has been submitted to a unified format code.

(4) no longer entangled in the i ++ or ++ i

  In the Go language, the increment operator is no longer an operator, but a statement. Therefore, in the Go language, the only way to write increment: i ++

  If written preincrement: ++ i, or increment the assignment: a = i ++, will result in a compilation error.

 

Reference

Several advantages and characteristics of the Go language   https://blog.csdn.net/u010429831/article/details/100096509

 

Guess you like

Origin www.cnblogs.com/yunfan1024/p/11416827.html