Part TWO: Go language foundation
2.1 Go language introduction
www.cnblogs.com/qfru/p/1060…
2.1.1 Go Language Introduction
Google’s open source
Too much of the Internet technology is open-source by Google
Go Core team
Cloud.tencent.com/developer/a…
Go Creation Background
Many of Google’s internal systems are completed in c++, but the compilation process is too slow, coupled with the complexity of the language itself, many of the language development process is constantly adding features,
Resulting in more and more bloated language, coupled with the development of the Internet, it is better to develop a separate language, the representative of this language is JS, patch language, the beginning is 7 days to come out
Go was originally intended as an alternative to C ++. As we’ll see later, Go advocates simplicity and efficiency: it inherits but eliminates many features that C ++ doesn’t use
Most of the major programming languages today are pre-2000:
c 1972
c++ 1983
python 1991
java 1995
php 1995
javascript 1995
Copy the code
In 2005, AMD released dual-core processor, and multi-core became popular, but now the mainstream programming language has released the go language development node
2.1.2 Core features of GO language
Static languages (compiled languages)
2.1.3 Advantages of GO language
Simple syntax - reference to a number of languages, you can see a lot of C, python and the shadow of high efficiency of development performance is good - catch up with and exceed Java concurrent programming compilation speed easy to deployCopy the code
2.1.4 What can GO do
Service side development gin Beego
Container virtualization DockerK8s Storage EtCD TiDB InfluxDB Groupcache blockchain Ethereum Fabric Microservice ecosystem is improvingCopy the code
2.1.5 Current development trend of GO
-
The development of GO in China is faster than in other countries
-
Domestic hiring demand is growing fast
Alibaba Tencent Baidu Bytedance B station Millet Didi Jingdong 360 Qiuyun Zhihu MeituanCopy the code
Dubo-go many domestic manufacturers began to support GO, go has become the first support language
2.2 Variables and Constants
2.2.1 How to Define variables
1 Undeclared variable
The var name type is the syntax for declaring individual variables.
- The first way is to specify the type of the variable and use the default value if no value is assigned
var name string
name ="bobby"
Copy the code
- In the second part, the Type inference was made according to the values.
If a variable has an initial value, Go will automatically be able to infer the type of the variable using the initial value. Therefore, if the variable has an initial value, the type in the variable declaration can be omitted.
var name ="bobby"
Copy the code
- Var (); var (); var (); var ();
var a int = 10
var b = 10
c : = 10
Copy the code
This way it can only be used in functions, not in global variable declarations and assignments
package main
var a = "bobby"
var b string = "imooc"
var c bool
func main(){
println(a, b, c)
}
Copy the code
Multivariable declarations
* * * *
- In the first case, the declaration and assignment are separated by commas, and the default value exists if no assignment is made
var name1, name2, name3 type
name1, name2, name3 = v1, v2, v3
Copy the code
- Second, direct assignment. The following variable types can be of different types
var name1, name2, name3 = v1, v2, v3
Copy the code
- Third, set types
var (
name string
age int
)
Copy the code
Note:
- Variables must be defined before they can be used
- Go is a static language that requires the same type of variable and assignment.
- Variable names must not conflict. (The same action domain cannot conflict)
- Short definition, at least one of the variable names on the left is new
- Short definition mode, cannot define global variables.
- The zero value of a variable. Also called the default value.
- If a variable is defined, it must be used, otherwise it will not compile.
We cannot use initialization declarations for variables with the same name again if they are in the same code block, for example: No new variables on left side of :=, but a = 20 is allowed because it will add a new value to the same variable.
If you use variable a before defining it, you get a compilation error undefined: a. If you declare a local variable but do not use it in the same code block, you will also get a compilation error, as in the following example:
func main() {
var name string = "bobby"
fmt.Println("bobby, imooc")
}
Copy the code
Attempting to compile this code will get error A declared and not used
It is not enough to simply assign a value to a. This value must be used, so if a variable with the same name exists in the same scope, subsequent declaration initializations will degenerate into assignment operations. However, at least one new variable must be defined in the same scope. For example, the following y is the newly defined variable
package main
import (
"fmt"
)
func main() {
x := 140
fmt.Println(&x)
x, y := 200, "abc"
fmt.Println(&x, x)
fmt.Print(y)
}
Copy the code
3 Anonymous Variables
When using multiple assignments, you can use the anonymous variable _ if you don’t need to receive a variable in an lvalue, which is often used in Python
For data, _ in range list {} //python data = [2,4,1,6] for _, item in enumerate(data): print(item)Copy the code
2.2.2 Definition of constant
1 a constant
A constant is an identifier of a simple value, an amount that will not be modified while the program is running.
Implicit type definition: const b = "ABC"Copy the code
package main import "fmt" func main() { const LENGTH int = 10 const WIDTH int = 5 var area int const a, b, c = 1, False, "STR" // multiple assignment area = LENGTH * WIDTH fmt.Printf(" %d", area) fmt.Println(a, b, c)}Copy the code
Constants can be enumerated, groups of constants
const (
Unknown = 0
Female = 1
Male = 2
)
Copy the code
If the type and initialization value are not specified in a constant group, it is the same as the rvalue of a non-empty constant on the previous line
package main
import (
"fmt"
)
func main() {
const (
x uint16 = 16
y
s = "abc"
z
)
fmt.Printf("%T,%v\n", y, y)
fmt.Printf("%T,%v\n", z, z)
}
Copy the code
Constant considerations:
- The data types in constants can only be Boolean, numeric (integer, floating point, and complex), and string
- Unused constants will not be reported at compile time
- When displaying the specified type, you must ensure that the left and right constant values are of the same type, and display type conversions can be performed if necessary. This is different from variables, which can be different types of values
2 iota
Iota, a special constant, can be thought of as a constant that can be modified by the compiler
Iota can be used as an enumeration value:
const (
a = iota
b = iota
c = iota
)
Copy the code
The first ioTA is 0, and its value is automatically incremented each time ioTA is used on a new line. So a=0, b=1, c=2 can be abbreviated as:
const (
a = iota
b
c
)
Copy the code
Package main import "FMT" func main() {const (a = iota //0 b //1 c //2 d = "ha", Iota +=1 e //"ha" iota +=1 f = 100 g //100 ioTA +=1 h = ioTA //7, recovery count I //8) fmt.Println(a,b,c,d,e,f,g,h,i) }Copy the code
If the IOTA increment is interrupted, it must be explicitly restored. And the subsequent self-increment increases in line order
Autoincrement default is int, can display the specified type
Numeric constants do not allocate storage space and do not need to be evaluated by memory addressing like variables, so they cannot obtain addresses
Using IOTA simplifies definitions and is useful when defining enums.
Iota is initialized to 0 each time a const occurs.
Const a= iota // a=0 const (b= iota //b=0 c=1)Copy the code
2.2.3 What are anonymous variables
1. How do you define anonymous variables in Python?
1. How do you define anonymous variables in Python?
My_list = ["bobby", "imooc", "test"] # print index for index, item in enumerate(my_list): print(item)Copy the code
Line 5 above does not print index. In fact, there are cases where we do not use the index variable. But in this case the index variable is not used but the name index is used, so we can use anonymous variables in this case. Change the code above to:
My_list = ["bobby", "imooc", "test"] index for _, item in enumerate(my_list): print(item)Copy the code
Change the index above to the underscore “_” to indicate that the variable here is an anonymous variable. This eliminates the need for variable names and provides an extra placeholder.
Note: Python does not declare variable names to be used in subsequent code, but go does declare variables to be used without an error, so anonymous variables are more commonly used in Go.
2. Define anonymous variables in go language
func test() (int, error) { return 0, nil } func main() { _, err := test() if err ! = nil {fmt.Println(" function call successful ")}}Copy the code
Note: Line 6 receives the return value from the function using an anonymous variable. Because we don’t print the returned value here, we just care if the function call was successful
2.2.4 Summary of this Chapter
Here is the finch document, click the link: www.yuque.com/bobby-zpcyu…
2.3 Principles of computer Rapid literacy
2.3.1 Why should we learn the principles of computer composition
- Why is the principle of computer composition important? Without the principles of the computer, the principles of the operating system would be hard to learn. I. What is Virtual memory II. How to schedule multiprocess and Multithreading III. What is Lock and Deadlock 4. If you don’t understand the operating system – you can’t understand the components -redis, ES, RabbitMQ etc. D. What are interrupts, and why do I/O operations actually cost no CPU? E. Many components and frameworks will change, but the principles of computer composition and operating system will not
- What are the knowledge of computer constitute principle book.douban.com/subject/123…
- The basic principles of computer composition that we will explain a. The most basic knowledge of computer composition that we will learn in the course B. Base and base conversion c. Some basics of ASCII code tables and memory
- Why are we doing this? A. Python shields us from a lot of low-level details. B. These are the details we need to know more about in a static language c. If the implementation does not know, it can be confusing to learn a static language
- How to learn computer composition principle These knowledge for us to remember, but you have to know about, don’t mention or online to see when other people give you information omitted these basic knowledge remember you don’t know so much not reality, when you see a lot of in-depth interpretation of the principle of the article will be more or less involves the principle of operating system and computer composition principle