Variable declarations

  • var s1 string = "hello"Specify the type
  • var s1 = "hello"Automatically inferring
  • s1 := "hello"Short for automatic inference

constant

Const PI = 3.1415926 Constant does not change after definition

const (
   name2 = "Zhang"  
   name3  // Also "zhang SAN"
   name4  // Also "zhang SAN"
   name5 = "Bill"
)
Copy the code

Iota is reset to 0 when the const keyword is present. Const adds ioTA every new line of constant declaration (ioTA is the row index in cons blocks). Iota simplifies definitions and is useful when defining enumerations (simply referred to as row indexes)

const (
   a = iota / / 0
   b / / 1
   c / / 2
   d / / 3
) 

const (
   a = iota / / 0
   b = iota / / 1
   _ / / 2
   d / / 3
)

const (
   a = iota / / 0
   b = 100  
   _ = iota  / / 2
   d   / / 3
)

const (
   a1, b1 = iota +1.iota +2  // a1 = 1 b1 = 2
   a2, b2 = iota +1.iota +2  // a2 = 2 b2 = 3
)

const(_ =iota
   KB = 1< < (10 * iota) 
   MB = 1< < (10 * iota)
   GB = 1< < (10 * iota)
   TB = 1< < (10 * iota))Copy the code

Basic types of

Basic types of describe
uint Unsigned integer 32-bit system yes uint32 64-bit system Yes uint64
int The signed integer is int32 for 32-bit systems and int64 for 64-bit systems
uintptr An unsigned integer used to hold a pointer

Floating point Numbers

  • float32 math.MaxFloat32The maximum value is 3.4E38
  • float64The default value of the minor number in go isfloat64.math.MaxFloat64The maximum value is 1.8E308

Boolean value

The default is false to disallow type conversions

An array of

var testArray [3]int                        // The array is initialized to a zero value of type int
	var numArray = [3]int{1.2}                 // Complete the initialization with the specified initial value
	var cityArray = [3]string{"Beijing"."Shanghai"."Shenzhen"} // Complete the initialization with the specified initial value
	fmt.Println(testArray)                      / / [0 0 0]
	fmt.Println(numArray)                       / / [1, 2 0]
	fmt.Println(cityArray)                      //[Beijing, Shanghai and Shenzhen]
Copy the code
a := [...]int{1: 1.3: 5}
fmt.Println(a)                  // [0 1 0 5]
fmt.Printf("type of a:%T\n", a) //type of a:[4]int
Copy the code
	var a = [...]string{"Beijing"."Shanghai"."Shenzhen"}
	// Method 1: for loop through
	for i := 0; i < len(a); i++ {
		fmt.Println(a[i])
	}

	// for range = index, value = value
	for index, value := range a {
		fmt.Println(index, value)
	}
Copy the code

Multidimensional array

        a := [3] [2]string{{"Beijing"."Shanghai"},
		{"Guangzhou"."Shenzhen"},
		{"Chengdu"."Chongqing"},
	}
	fmt.Println(a) //[[Beijing Shanghai] [Guangzhou Shenzhen] [Chengdu Chongqing]]
	fmt.Println(a[2] [1]) // Index Value: Chongqing
Copy the code

traverse

Func main () {a: = [3] [2] string {{" Beijing ", "Shanghai"}, {" guangzhou ", "shenzhen"}, {" chengdu ", "chongqing"},} / / v1 for _, v1: the range of = a {for _, v2 := range v1 { fmt.Printf("%s\t", v2) } fmt.Println() } }Copy the code

Note: Only the first layer of a multidimensional array can be used… To let the compiler derive the array length. Such as:

// A := [...] [2] string {{" Beijing ", "Shanghai"}, {" guangzhou ", "shenzhen"}, {" chengdu ", "chongqing"},} / / does not support the lining of the multi-dimensional array using... b := [3][...] String {{" Beijing ", "Shanghai"}, {" guangzhou ", "shenzhen"}, {" chengdu ", "chongqing"},}Copy the code

Slice and map are initialized using make first

slice1 := make([]int.10) 
mapa := make(map[string]string)
Copy the code

The structure of the body

Structures can be capitalized by other modules, as can fields

type Cat struct {
   name  string
   age   int
   color string
}
Copy the code

Define methods for Cat

Type Cat struct {name string age int color string Animal} func (Cat Cat) getName() string {return Cat c := Cat{name: name2} println(c.getName())Copy the code

interface

  • Reference type, nil if not initialized
  • An empty interface with no methods, which all structures implement by default

Type judgment

Var a1 interface{} var b Teacher = Teacher{} // ok is a bool if _, ok := a1. ok { }else { fmt.Println(ok) }Copy the code