Public number: swimming code sea there are more boutique original articles ~

Why learn Go?

  1. The perfect blend of development efficiency and operational efficiency, with natural concurrent programming support.
  2. The complete standard library includes Internet applications, system programming and network programming.
  3. It’s very easy to deploy
  4. Has a powerful compiler check, strict coding specifications and complete software life cycle tools

compiler

Method 1: Run directly

go run xxxx.go
Copy the code

Mode 2: Build mode

go build xxx.go
Copy the code

Variable declarations

Variables in Go are declared with type information after the variable

/ / / is as follows:
var v1 int 
var v2 string
var v3 [10]int  / / array
var v4 []int 	// Array slice
var v5 struct {
	f int
}
var v6 * int
var v7 map[string] int  // Map type The key is string and the value is int
var v8 func(a int) int

// var You can put the var keyword in curly braces so that you don't have to write var again
var {
    v1 int
    v2 string
}
Copy the code

How a variable is initialized

var v1 int = 10
var v2 = 10  // The type can be inferred automatically
v3 := 10   // A variable can be initialized like this
// Disadvantages: cannot provide data types, only inside functions
Copy the code

Variables declared for all three types should not be declared otherwise a compilation error will occur

Avoid trying to modify a global variable and accidentally defining a new local variable.

Variable assignment

// A new type of assignment
i , j = j , i   // Can be used to swap two variables

// We can provide anonymous variables when we accept the return value of a function

func GetName(a) (firstName, lastName, nickName string){
	return "MAy" , "Chan" , "Chibi Maruko"
}

// get the nickName
_, _, nickName := GetName
Copy the code

The degradation of the assignment

Prerequisite: At least one new variable is defined and must be of the same scope

func main(a){
	x := 100... : x =200  // No degenerate assignment occurs until at least one new variable is defined
    
    //x,y :=200, 300
    {
       //x,y := 200, 300 // Not in the same scope, no degenerate assignment}}Copy the code

Err variables usually use degenerate assignments to achieve repeated use

constant

Constant types in Go can be numeric types, etc

// Const can be used to define constants
const pi float64 = 3.1415
cosnt zero = 0.0
const  {
	size int64 = 10
	eof = - 1
}

const u , v  float32 = 0.3
const a, b , c = 3 , 4 , "foo"

cosnt mask = 1 << 3  // This is ok because it can be determined at compile time

// Predefined constants true false iota
//iota is a value that increments by 1 each time it occurs and is reset to 0 at the beginning of each const

cosnt {
    c0 = iota	/ / 0
    c1 = iota  	/ / 1
    c3 = iota	/ / 2
}
/ / or
const{
    c0 = iota	/ / 0
    c1			/ / 1
    c3			/ / 2
}

cosnt x = iota / / 0
const y = iota / / 0
Copy the code

A Boolean type cannot be converted to another type

Two different types of integers cannot be compared

The inverse is ~x in C, ^x in GO

Floating-point numbers are not an exact representation so don’t directly compare functions IsEqual()Function to compare

The plural types

var value complex64  // Is actually a complex type composed of float32

// Three types of assignment
value = 3.2 + 12i
value := 3.2 + 12i
value := complex(3.2.12)

// We can use real(value) to get the real part
// You can use imag(value) to get the imaginary part
Copy the code

String type

The ** string can be initialized, but unlike an array, it cannot be changed again or a compilation error will occur, but the following table can be evaluated as an array

We often use the len function to get the length of a string

Go supports UTF-8 encoding, so Chinese and English can be mixed together. The source file should be saved in UTF-8 format

We often use the iconv library to work with text documents

Each Chinese character takes up 3 bytes in UTF-8

// Two traversal modes

// Traversal as an array of characters
str := "Hello, world"
n := len(str)
for i := 0 , i < n , i++ {
	ch := str[i]
}
// Unicode traversal
for i , ch := range str{
	fmt.Println(i, ch)
}
Copy the code

Reference types

Silice, Map, and Channel are predefined types

In addition to allocating memory, reference types initialize a number of properties, such as Pointers, lengths, and even hash distributions and data queues

The new function allocates memory for reference types, but this is not completely created. Only memory for the data type itself (pointer wrapper) is allocated, not memory for the key-value store, and no internal attributes are initialized

Type conversion requirements: Explicit type conversion

Custom type

Use the type keyword to define user-defined types, including those created based on existing base types, structs, function types, and so on

Note: Even if the base type is specified, it only appears that they have the same underlying data structure, there is no relationship between the two, and they are completely different types. Except for operators, custom types do not inherit other information (including methods) from the underlying type. Cannot be used as aliases, cannot be converted implicitly, and cannot be used directly to compare expressions

type flags byte
const {
    read flags = 1 << itoa
    write
    exec
}
func main(a) {
    f := read | exec 
    fmt.Printf("%b\n",f) 		// Outputs binary flag bits
}
Copy the code

To be continued...