Go study notes

Golang Tip: after writing the article, the di...
1. Directory structure
2. Code structure
1.Go execution process analysis
2. Syntax considerations
3. Code style
4.API (source code search)
5. Variables
6. Basic data type
7. Complex data types
8. Identifier
9. Operator
10.switch
11.for range
12. Function
13. Package
14. System function
15. Exception handling
16. Object oriented
Golang

Tip: after writing the article, the directory can be generated automatically. For how to generate it, please refer to the help document on the right

preface

Tip: Here you can add the general contents to be recorded in this article:
For example, with the continuous development of artificial intelligence, machine learning technology is becoming more and more important. Many people have started learning machine learning. This paper introduces the basic content of machine learning.

Tip: the following is the main content of this article. The following cases can be used for reference

1, Golang environment

1. Directory structure

2. Code structure

go build test.go compile go file to generate exe file
test.exe

go run is equivalent to two steps, compiling and executing

2, Golang Foundation

1.Go execution process analysis

The code is as follows (example):

  1. One can generate an EXE file and then execute the exe.


The compiled file will be larger because some dependent library files are put into the executable file

  1. The second can be compiled and run directly.

The difference is that the first method can execute exe on a machine without go development environment.

2. Syntax considerations


If the imported package is not used, go build will fail.

3. Code style


4.API (source code search)

5. Variables

package main import "fmt" func main() { var age int age = 18 fmt.Print(age) } Declaring a variable is equivalent to opening up a space in memory for it

A. Four forms of use


Global variable declaration

6. Basic data type

A. Integer type

I. signed integer type

package main import "fmt" func main() { var num int8 = 321 fmt.Println(num) } D:\gowork\src\unit2\demo01>go build main.go # command-line-arguments .\main.go:12:6: constant 321 overflows int8
II. Unsigned integer type


Golang's default integer type is int

III. other integer types

%T View type func main() { var num3 = 28 fmt.Printf("%T", num3) fmt.Println() fmt.Println(unsafe.Sizeof(num3)) } D:\gowork\src\unit2\demo01>main.exe int 8


For example, in java, it is normal to use int for age. In fact, int takes up too much byte space. You can use byte.
Use uint8 in go, unsigned bit int 0 - 255

B. Floating point type

  • Floating point underlying storage space is independent of the operating system
  • The bottom storage of floating-point type: sign bit + exponential bit + mantissa. Only the mantissa position is stored, and there may be precision loss
func main() { var num float32 = 314e-2 //That's 314 times 10 to the negative quadratic fmt.Println(num) var num1 float32 = 314e+2 //That's 314 times 10 to the power of two fmt.Println(num1) var num2 float32 = 256.0000000916 fmt.Println(num2) } D:\gowork\src\unit2\demo01>main.exe 3.14 31400 256 You can see that the accuracy is lost The default type is float64

C. Character type

func main() { var c1 byte = 'a' fmt.Println(c1) var c2 byte = '6' fmt.Println(c2) var c3 byte = '(' fmt.Println(c3) } ASCII code D:\gowork\src\unit2\demo01>main.exe 97 54 40

D.Boolean type

E. String type


Escape double quotes with special characters

F. Default value of basic data type

G. Explicit conversion of data type



n1 above is still of type int, but the value of n1 is converted to float32


Data overflow when converting large type to small type

When adding multiple data types, pay attention to the consistency of data types

+128 exceeds the range of int8

H. Data type to String


fmt.Sprintf(%? n)
%? Data type corresponding to n

1. String to basic data type


Ignore err errors.

7. Complex data types

A. Pointer


&age
View variable memory address

func main() { var age int = 15 fmt.Println(&age) //var stands for declared variable //ptr pointer variable name //*Int pointer type (pointer to int type) //&Age memory address var ptr *int = &ag }


I. change the pointing value through the pointer

II. The pointer variable must accept the address value

III. the address of the pointer cannot be mismatched

PS: *float It means that the pointer points to float32,however&num Corresponding to int Type is not allowed.
Ⅳ. Basic data types (value types) have corresponding pointer types in the form of * data type.

B. Array

I. Introduction of arrays

Ⅱ. Memory analysis of arrays


III. traversal of arrays


Ⅳ. Array initialization

V. precautions


C. Slice

I. Introduction of slices


Ⅱ. Memory analysis of slices

III. define a slice

Ⅳ. Precautions for slicing






D.Map

I. Introduction of Map


II. Three creation methods of Map

III. Map operation

8. Identifier

9. Operator

10.switch



Penetrate one layer

11.for range


12. Function

A. Function reference

func cal (num1 int ,num2 int)(int){ var sum int = 0 sum += num1 sum += num2 return sum }

B. Function name, formal parameter and argument


Accept two parameters

C. Function memory analysis


D. Function details

I. function overloading is not supported

II. Variable parameters are supported in Golang

III. basic data types and arrays are value passing by default, that is, copy only, and then modify the function (stack frame)

Ⅳ. Using & value transfer

V. in Go language, function is also a data type


Vi. in Go, functions can be used as formal parameters and called

VII. You can customize the data type in Go

Ⅷ. The order can be ignored when naming the parameters of the function

IX. init function



Execution sequence

X. anonymous functions

Xi. Closure


The variable referenced by the anonymous function will always be saved in memory and can always be used

XII. defer keyword


13. Package

A. Package introduction

B. Package details


14. System function

A. String function



B. Date function


C. Built in function

I. new function


II. make function

15. Exception handling

A.defer+recover capture


B. Custom error


16. Object oriented

A. Definition of structure

B. Structure instance creation

C. Conversion between structures


15 September 2021, 00:47 | Views: 3147

Add new comment

For adding a comment, please log in
or create account

0 comments