Introduction to Extreme Go Language (Super Full and Super Detailed) - Basic

Article Contents

Golang Overview

Three Bulls of Go Language

Why Google created Golang

Development of Golang

In 2007, Google engineers Rob Pike, Ken Thompson and Robert Griesemer began to design a new language, which is the original prototype of Go language.

On November 10, 2009, Google released the Go language to the world in an open source manner.

On August 19, 2015, Go version 1.5 was released, and the "last remaining C code" was removed in this update

On February 17, 2017, Go language Go 1.8 was released.

On August 24, 2017, Go language Go version 1.9 was released. 1.9.2 Version

On February 16, 2018, Go language Go version 1.10 was released.

Features of Golang's Language

Go language ensures the safety and performance of static compiling language, and also achieves the efficiency of dynamic language development and maintenance. An expression is used to describe Go language: Go=C+Python, which shows that Go language not only has the running speed of C static language program, but also can achieve the rapid development of Python dynamic language.

Inherited many ideas from C language, including expression syntax, control structure, basic data type, calling parameter value, pointer, etc

It also retains the same compilation and execution mode and weakened pointer as the C language

Take a case (experience):

//Characteristics (experience) of pointer use in go language

func testPtr(num *int) {

*num = 20

}

2. The concept of package is introduced to organize the program structure. A file in Go language should belong to a package and cannot exist alone.

Package main//A file belongs to a package

import "fmt"

func main() {

fmt.Println("hello word")

}

Garbage collection mechanism, automatic memory collection, no developer management

Natural concurrency (important features)

It supports concurrency from the language level and is easy to implement

Goroutine, a lightweight thread, can realize large concurrent processing and efficiently use multiple cores.

Implementation based on CPS concurrency model (Communicating Sequential Processes)

Absorbed the pipeline communication mechanism to form a pipeline channel unique to Go language. Through the pipeline channel, different goroutes can be realized

Mutual communication between.

Functions can return multiple values. give an example:

//Write a function to return sum and difference at the same time

//The go function supports returning multiple values

func getSumAndSub(n1 int, n2 int) (int, int ) {

Sum:=n1+n2//The go statement should not be followed by a semicolon

sub := n1 - n2

return sum , sub

}

New innovations: such as slice, deferred execution

Go language development tool

I use the GoLand development tool, which is quite handy

GoLand is the Go language integrated development environment launched by JetBrains. GoLand is also developed based on the IntelliJ platform and supports the plug-in system of JetBrains.

Download address: https://www.jetbrains.com/go/download/other.html

I choose the version I like and download it from the corresponding system. It is not recommended to use a newer version. I can easily use the 2021.3.5 version myself

If you have an account, you can go to Taobao to buy an education registration channel, which can be used for one year after registration

Or use the plug-in for 30 days( ⚠️ Note that 21 years and earlier can be used, but the newer version does not support it). The relevant document address is: https://blog.csdn.net/qq_37699336/article/details/116528062

Go development environment configuration (sdk download and configuration)

SDK download address: Golang China https://www.golangtc.com/download

Reference documents related to sdk configuration: https://blog.csdn.net/m0_49589654/article/details/127259754

Use development tools to create the first Go project

Create a project and select the path of the sdk

Right click the project file to create a go file

Then write a hello word

Package main//A file belongs to a package

import "fmt"

func main() {

fmt.Println("hello word")

}

function

Precautions for Go program development

Go source files have the extension "go".

The execution entry of the Go application is the main () function. This is similar to other programming languages (such as java/c)

The Go language is strictly case sensitive.

The Go method is composed of statements, and no semicolon is required after each statement (Go language will automatically add a semicolon after each line), which also reflects the simplicity of Golang.

The Go compiler compiles one line at a time, so we can write one statement per line. We can't write multiple statements in the same one. No

Then an error is reported

If the variables defined in the go language or the import package are not used, the code cannot be compiled.

Brackets appear in pairs and are indispensable.

Official reference documents

Golang official website (may not be accessible): https://golang.org

Online standard library document of Golang Chinese website: https://studygolang.com/pkgdoc

Go Learning

Go variable

Concept: A variable is equivalent to the representation of a data storage space in memory. You can think of a variable as the house number of a room. We can find the room through the house number. Similarly, we can access the variable (value) through the variable name.

Steps to use variables: declare variables (also called: define variables) ->assign non variables ->use variables

Integer data

It is divided into two types: signed and unsigned

Signed: int, int8, int16, int32, int64

Unsigned: uint, uint8, uint16, uint32, uint64, byte

The difference between integer types of different digits lies in the size of the range of integer numbers that can be saved;

The signed type can store any integer, and the unsigned type can only store natural numbers

The sizes of int and uint are related to the system. 32-bit systems represent int 32 and uint 32, and 64 bit systems represent int 64 and uint 64

Byte, similar to uint8, is generally used to store a single character

Try to use data types that occupy less space while ensuring the correct operation of the program

Fmt. Printf ("% T", var_name) output variable type

Unsafe. Sizeof (var_name) View the bytes occupied by variables

float

Floating point type, also known as decimal type, can store decimals. For example, 6.6, -12.34

1. A brief description of the storage form of floating point numbers in the machine. Floating point numbers=sign bits+exponent bits+tail bits

2. The mantissa may be lost, resulting in loss of accuracy- one hundred and twenty-three point zero zero zero zero nine zero one

3. Floating point storage is divided into three parts: sign bit+exponent bit+tail bit. During storage, precision will be lost

4. The floating point type of golang defaults to float64

5. In general, you should use float64 because it is more accurate than float32

6. 0.123 can be abbreviated to. 123. It also supports the Scientific notation to indicate that 5.1234e2 is equivalent to 512.34

character

There is no special character type in Golang. If you want to store a single character (letter), you usually use byte to save it.

A string is a sequence of characters connected by a string of fixed length characters. The Go string is connected by a single byte, that is to say, the traditional string is composed of characters, but the Go string is different, it is composed of bytes.

Characters can only be wrapped by single quotation marks, not double quotation marks. Double quotation marks wrap strings

When we directly output the type value, we output the ASCII code value of the corresponding character

When we want to output the corresponding characters, we need to use formatted output

Characters in Go language are encoded in UTF-8, with English letters accounting for one character and Chinese characters for three characters

In Go, the character is an integer in nature. When it is directly output, it is the UTF-8 encoded code value corresponding to the character.

You can directly assign a number to a variable, and then press% c to output the unicode character corresponding to the number

Character types can be calculated, which is equivalent to an integer, because they all have corresponding unicode codes

Discussion on the nature of character types

When character type is stored in the computer, the code value (integer) corresponding to the character needs to be found and stored: character -->code value -->binary -->storage and reading: binary -->code value -->character -->reading

The correspondence between characters and code values is determined by the character code table (it is stipulated)

The coding of Go language is unified into UTF-8. It is very convenient and unified. There is no confusion about coding anymore.

Boolean

Boolean type is also called bool type. Only true or false values are allowed for bool type data

Bool type occupies 1 byte

The bool type is applicable to logic operations and is generally used for process control

character string

A string is a sequence of characters connected by a string of fixed length characters. The Go string is concatenated by a single byte. Bytes of strings in Go language use UTF-8 encoding to identify Unicode text

1. Once the string is assigned, it cannot be modified: in Go, the string is immutable.

2. Two identification forms of string

Pointer

Basic data type. Variables store values, also called value types

Get the address of the variable. Use&, such as var num int, to get the address of num:&num

Pointer type. The pointer variable stores an address, and the space pointed to by this address stores a value, such as var ptr * int=&num

Get the value pointed to by the pointer type, use: *, for example, var ptr int, and use ptr to get the value pointed to by ptr

Pointer details:

Value types have corresponding pointer types in the form of data types. For example, the pointer corresponding to int is int, the pointer corresponding to float64 is * float64, and so on.

Value types include: basic data type, array and struct

Value type and reference type

Use characteristics of value type and reference type:

Value type: variables directly store values, and memory is usually allocated in the stack

Reference type: A variable stores an address. The space corresponding to this address really stores data (values). Memory is usually allocated on the heap. When no variable applies this address, the data space corresponding to this address becomes a garbage, which is recycled by GC.

Distinguishing between Golang median type and reference type

Value type: basic data types (int series, float series, bool, string), arrays, and structures

Reference type: pointer, slice slice, map, pipe chan, interface, etc. are reference types

identifier

concept

The character sequence used by Golang to name various variables, methods, functions, etc. is called an identifier. Any place where Golang can name itself is called an identifier.

Naming rules for identifiers

It is composed of 26 English letters, 0-9_ form

The number cannot start. var num int //ok var 3num int //error

Golang is strictly case sensitive.

In golang, num and Num are two different variables (var num int

,var Num int )

Identifier cannot contain spaces.

The underscore "_" itself is a special identifier in Go, called null identifier. Can represent any other identifier, but

The corresponding value will be ignored (for example, a return value will be ignored). Therefore, it can only be used as a placeholder, not an identifier

You cannot use system reserved keywords as identifiers (25 in total), such as break, if, etc

Details

The results of relational operators are bool type, that is, they are either true or false.

Expressions composed of relational operators are called relational expressions: a>b

The comparison operator "==" cannot be written as "="!!

Logical operator

It is used to connect multiple conditions (usually relational expressions), and the final result is also a bool value

1) &&is also called short circuit and: if the first condition is false, the second condition will not be judged, and the final result is * * false**

2. | | Also called short circuit or: if the first condition is true, the second condition will not be judged, and the final result is true

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us