GO - if determining, for loop, a switch statement, the array

1.if - else if - else的使用

package main

import "fmt"

func main() {
    // 1.简单使用
    var a=10
    if a==10 {  // 这个位置不能回车,不能换行
        fmt.Println(a)
    }

    //2 if - else
    var b =16
    if b==20 {
        fmt.Println("20")
    }else{
        fmt.Println("15")
    }
    
    //3 if - else if - else
    var c =20
    if c==20 {
        fmt.Println("20")
    }else if c==15 {
        fmt.Println("15")
    }else {
        fmt.Println("10")
    }
}

2.for cycle

package main

func main() {
    // 基本语法
    for 初始化; 条件; 自增/自减{
        
    }
    
}

2.1 simple to use

    for a:=0; a<10; a++  {
        fmt.Println(a)
    }

2.2 Initialization omission

    a :=0
    for ;a<10 ; a++ {
        fmt.Println(a)
    }

2.3 Initialization increment and decrement are omitted

    a :=0
    for ;a<10 ;  {
        fmt.Println(a)
        a++
    }

2.4 Initialization increment and decrement are omitted (Premium)

Note: two semicolons omitted (similar to the while loop)

    a :=0
    for a<10  {
        fmt.Println(a)
        a++
    }

2.5. Infinite loop

    // 死循环一
    for true{
        fmt.Println("ak47")
    }

    // 死循环二(三部分都省略了)
    for {
        fmt.Println("ak47")
        break
    }

3.switch statement

3.1 Basic use

package main

import "fmt"

func main() {

    // 1.基本使用
    var a = 10
    switch a {
    case 1:  //相当于if a==1
        fmt.Println("1")
    case 5:
        fmt.Println("5")
    case 10:
        fmt.Println("10")
    }
}

3.2default

    var b = 18
    switch b {
    case 1:
        fmt.Println("1")
    case 5:
        fmt.Println("5")
    case 10:
        fmt.Println("10")
    default:   //相当于else
        fmt.Println("你是蔡徐坤")
    }

More than 3.3 expression judgment

(Provided that they meet the conditions on the establishment of a period)

    var c = 18
    switch c {
    case 1, 2, 5:
        fmt.Println("1")
    case 6, 7:
        fmt.Println("5")
    case 10, 15, 18:
        fmt.Println("10")
    default:
        fmt.Println("你是蔡徐坤")
    }

3.4 free expression

    var d = 30
    switch {
    case d == 20:
        fmt.Println("1")
    case d == 30:
        fmt.Println("5")
    case d == 40:
        fmt.Println("10")
    default:
        fmt.Println("你是蔡徐坤")
    }

3.5 free expression

and (and) && or condition (or) condition ||

    var e = 20
    var d = 30
    switch {
    case d == 30 && e==20:   //同时成立
        fmt.Println("1")
    case d == 30:
        fmt.Println("5")
    case d == 40:
        fmt.Println("10")
    default:
        fmt.Println("你是蔡徐坤")
    }

3.6.fallthrough

Unconditionally to the next case (penetration)

    var f = 2
    switch  {
    case f == 1:
        fmt.Println(1)
    case f == 2:
        fmt.Println(2)
        fallthrough   //一旦碰到 fallthrough会无条件执行下面的语句
    case f == 6:
        fmt.Println(250)
        fallthrough   ////只要代码读到fallthrough与他紧挨着的语句,无论是否满足条件,他都会执行里面的内容
    case f == 3:
        fmt.Println(666)
    }

4. Array

4.1 Definitions Array

package main
import "fmt"
func main() {
    // 同一类元素的集合, 不允许混合不同元素
    // 定义数组(定义了一个大小为4的int类型数组)
    var a [4]int  // 四个为0的数组
    fmt.Println(a)  // int默认值为0
}

4.2 Using arrays

(Starting at index 0)

package main
import "fmt"
func main() {
    var a [4]int 
    fmt.Println(a) 
     a[0] = 10
    fmt.Println(a[0])
    fmt.Println(a)
}

4.3 defined and assigned an initial value (in three ways)

package main
import "fmt"
func main() {
    // 1.
    //var b [4]int = [4]int{1,2,3,4}
    // 2.
    //var b = [4]int{1,2,3,4}
    // 3.
    b := [4]int{1,2,3,4}
    fmt.Println(b)
}

4.4 Defining and initial value (otherwise)

package main
import "fmt"
func main() {
    //var c [4]int = [4]int{1,2}  // 只给前两个赋值
    //var c [4]int = [4]int{1:2}  // 只给第二个赋值,前面是索引,后面是值
    //var c [4]int = [4]int{1:2,2:3}  // 只给第二,三个个赋值
    var c [4]int = [4]int{2:3,1:2}  // 只给第二,三个个赋值
    c[3] = 100
    fmt.Println(c)
}

4.5 a fixed array size (size is defined once, can not be changed)

package main
import "fmt"
func main() {
    //var c [4]int = [5]int{1,2}   // 错的
    //var c [4]int = [4]int{1,2,3,4,5}   // 错的

    //var c [4]int = [4]int{1,2,3,4}
    //a[4] = 10  //错的,最大索引为3
    //fmt.Println(a[4])  //错的
}

4.6 The size of the array is part of the type

package main
import "fmt"
func main() {
    // 下面两个不是同一种类型(不能比较和加减)
    var a [3] int
    var b [4] int
}

4.7. An array of value types (reference types)

// is not accurate (py because everything is in the object, immutable data types are objects, objects that address)

Immutable type (Type value) // py as the parameter to the function, if the value is changed in the function, it will not change the original value

// py type of variable (reference types: address) as a parameter to the function, if the change in the function value, the original value will change

// go type in value, when passed as a parameter, a copy in the past, the value is equivalent to copy is a copy past

package main
import "fmt"
func main() {
    var d = [4]int{1,2,3,4}
    test(d)  // 当做参数传递过去
    fmt.Println(d) // 原值没有改变
}

func test(d [4]int)  {
    d[0] = 100   // 修改值
    fmt.Println(d) // 传过来的数组改变了,但原值不变
}

4.8 the array length (len)

package main
import "fmt"
func main() {
    var e = [4]int{1,2,3,4}
    fmt.Println(len(e))
}

An array of cycle 4.9

package main
import "fmt"
func main() {
    // 方法一:
    var a = [4]int{1,2,3,4}
    for i:=0 ;i<len(a) ;i++  {
        fmt.Println(a[i])
    }
    
    //方式二:(range关键字,它不是内置函数)把数组放在range关键字后
    var b = [4]int{1,55,3,66}
    for z:=range b{
        //fmt.Println(z)
        fmt.Println(b[z])  //这不是正统的用法
    }
    
    // 当用一个变量来接收时,这个变量就是索引
    // 当用两个变量来接收时,这两个变量就是索引和值
    for k,v:=range a {
        fmt.Println(k,v)
    }
    
    // 当你不想取索引的时候也可以用下划线代替
    for _,v:=range a {
        fmt.Println(v)
    }
}

4.10 multidimensional arrays

package main
import "fmt"
func main() {
    var a [4][3]int
    fmt.Println(a)
    a[1][1] =100  // 修改值
    fmt.Println(a)
}

4.11 multidimensional array defined and initial value

package main
import "fmt"
func main() {
    var a = [4][3]int{{1,1,1},{2,2,2},{3,3,3},{4,4,4}}
    fmt.Println(a)
    
    // 把数组第二个位置初值改为[6,6,6]
    var b = [4][3]int{1:{6,6,6}}
    fmt.Println(b)
    
    var c = [4][3]string{1:{"6","6","6"}}
    fmt.Println(c)
    // 字符串的空值是空字符串  ""
    // int类型空值是  0
    // 数组的空值,取决于类型的空值
    var d [4][3]string
    fmt.Println(d)
}

Guess you like

Origin www.cnblogs.com/guapitomjoy/p/12406026.html