01 Introduction to Golang, environment installation and sample programs

Language introduction

Generation and development

The core R&D team has three big cows: Ken Thompson, Rob Pike, Robert Griesemer

The reason why Google created Golang:

  1. Computer hardware is updated frequently, performance is improved quickly, and mainstream programming languages ​​cannot take advantage of multi-core and multi-CPU.
  2. The complexity of software programming is getting higher and higher, and the maintenance cost is getting higher and higher, lacking a sufficiently concise and efficient language
  3. Although the C/C++ project runs very fast, there is a memory leak problem.
    Summary: The current mainstream programming languages ​​are either not concise enough in development, operation and maintenance, or they are not efficient enough (cannot make full use of multi-core and multi-CPU), or they have memory leaks.

The development of Golang:

  1. In 2007, Google engineers Ken Thompson and Rob Pike developed and designed the initial prototype of Golang.
  2. On November 10, 2009, Google released Golang and open sourced it
  3. On August 19, 2015, Go 1.5 was released, removing the last C code that participated
  4. On February 17, 2017, Go language 1.8 was released,
  5. Released on February 16, 2018, 1.10

Features
Go language skills achieve the security and performance of static programming languages, and also have the high efficiency characteristics of dynamic development language development and maintenance. Expressed in an expression is: C+Python (that is, there is the efficiency of C and the efficiency of Python development and operation)

  1. Inherited many concepts from the C language, including syntax, control structure, basic data types, parameter transfer, pointers, etc.
  2. Introduce the concept of package, program files cannot exist alone, they must belong to a package
  3. Introduce a garbage collection mechanism, the memory is automatically just recycled, no developer management is required
  4. Natural support for concurrency Supports concurrency
    from the language level, implements simple
    goroutines, is lightweight and ready-made, can achieve large concurrent processing, and efficiently utilizes multi-core Implementation
    based on the CPS concurrency model (Communicating Sequential Processes)
  5. The pipeline communication mechanism is absorbed to form a unique pipeline of the Go language, and the communication between different goroutes is realized through the pipeline.
  6. Functions can return multiple values
  7. There are some new innovations, such as: slice slice, delay execution defer

Application field

  1. Blockchain, distributed ledger technology, allowing everyone to participate in database records
  2. Back-end application services, support background traffic, and run statistics. Such as the Meituan background flow support program.
  3. Cloud computing/cloud service background applications, such as Shanda CDN, JD messaging cloud service

Environmental installation

Program download

Self download install vscode
download golang SDK Download

Configuration and testing

Unzip the compressed package to the specified directory (the directory path does not contain Chinese and spaces) my path here is D:\greenPro\go
Configure three environment variables:

GOROOT:  D:\greenPro\go    goSDK解压后的跟路径
Path: D:\greenPro\go\bin        goSDK解压后跟路径下的bin 路径
GOPATH:  D:\works\goworks  程序开发使用的路径    

win+R Run the command line to execute go and see the following information to indicate that the installation is successful
Insert picture description here

Sample program writing and running

The content of Hello.go written by vscode under the above GOPATH path is as follows

package main
import "fmt"
func main(){
    
    
	fmt.Println("hello world!");
}

Enter the directory where Hello.go is located, and execute the following command to execute the effect as follows

D:\works\goworks\go01\main> go run Hello.go
hello world!

You can also compile first and then execute as follows

PS D:\works\goworks\go01\main> go build Hello.go  //编译完成后目录下出现 Hello.exe 的可执行文件
PS D:\works\goworks\go01\main>./Hello  //运行可执行文件
hello world!

After the code is compiled, the executable file is copied to the machine without the go environment, and it can still be run. Because the content needed for the program to run is packaged into the executable file when compiling

Other matters needing attention

Annotation

Line comment

 // 这行是注释

Block comment

 /*
这里是注释
*/

Special grammar rules

  1. If the imported package is not used, or the variable is not used after the variable is defined, an error will be reported.
  2. The program execution entry is the main function
  3. Strictly case sensitive
  4. There is no need for a semicolon after each statement (Go will automatically append a semicolon at the end of each line)
  5. The Go compiler compiles line by line, so we write one statement per line, and multiple statements cannot be written on the same line, otherwise an error will be reported
  6. The following wording will report an error
func main()//大括号必须在这个位置
 {
    
     //
 }

Code style

  1. Officially recommend line comments to write comments
  2. The correct indentation and white space can be adjusted by shift+Tab operation, and gofmt can be used for code formatting gofmt -w hello.go
  3. Add 1 space on both sides of the operator
  4. No more than 80 characters in a line, use line breaks to display more than 80 characters as elegantly as possible

Guess you like

Origin blog.csdn.net/zhangxm_qz/article/details/114357769