流程控制語句

if

if 5 > 9 {
    fmt.Println("5>9")
}
  • 如果邏輯表達式成立,就會執行{}里的內容。
  • 邏輯表達式不需要加()。
  • "{"必須緊跟在邏輯表達式后面,不能另起一行。
if c, d, e := 5, 9, 2; c < d && (c > e || c > 3) { //初始化多個局部變量。復雜的邏輯表達式
    fmt.Println("fit")
}
  • 邏輯表達中可以含有變量或常量。
  • if句子中允許包含1個(僅1個)分號,在分號前初始化一些局部變量(即只在if塊內可見)。

if-else的用法

color := "black"
if color == "red" { //if只能有一個
    fmt.Println("stop")
} else if color == "green" {
    fmt.Println("go")
} else if color == "yellow" { //else if可以有0個、一個或者連續多個
    fmt.Println("stop")
} else { //else有0個或1個
    fmt.Printf("invalid traffic signal: %s\n", strings.ToUpper(color))
}
//如果成立則輸出,否則進行下個判斷

if表達式嵌套

if xxx {
    if xxx {
    }else if xxx{
    }else{
    }
}else{
    if xxx {
    }else{
    }
}

注意太深的嵌套不利于代碼的維護,比如

//第一個條件為真則進行下次判斷,否則推出循環。其中有一個為假則退出。
var a, b, c, d, e, f = 1, 2, 3, 4, 5, 6

func main() {
    // if-else語句
    if a < b {
        if d < c {
            if e < f {
                fmt.Println("shabi")  //太深的嵌套不利于代碼維護
            }
        }
    }
   修改后
    if a>b{
        return 
    }
    if c>d{
        return 
    }
    if e<=f{
        fmt.Println("11s")

    if value, exsits := 1, true; exsits {
        fmt.Println(value)

    }
}

switch

color := "black"
switch color {
case "green" :  //相當于  if color== "green"
    fmt.Println("go")
case "red" :        //相當于else if color== "red" 
    fmt.Println("stop")
default:         //相當于else 
    fmt.Printf("invalid traffic signal: %s\n", strings.ToUpper(color))
}
  • switch-case-default可能模擬if-else if-else,但只能實現相等判斷。
  • switch和case后面可以跟常量、變量或函數表達式,只要它們表示的數據類型相同就行。
  • case后面可以跟多個值,只要有一個值滿足就行。
func add(a int) int {
    return a + 10
}

func switch_expression() {
    var a int = 5
    switch add(a) { //switch后跟一個函數表達式
    case 15: //case后跟一個常量
        fmt.Println("right")
    default:
        fmt.Println("wrong")
    }

    const B = 15
    switch B { //switch后跟一個常量
    case add(a): //case后跟一個函數表達式
        fmt.Println("right")
    default:
        fmt.Println("wrong")
    }
}
func switch_condition() {
    color := "yellow"
    switch color {
    case "green":
        fmt.Println("go")
    case "red", "yellow": //用逗號分隔多個condition,它們之間是“或”的關系,只需要有一個condition滿足就行
        fmt.Println("stop")
    }

空switch

switch后帶表達式時,switch-case只能模擬相等的情況;如果switch后不帶表達式,case后就可以跟任意的條件表達式

//switch后帶表達式時,switch-case只能模擬相等的情況;如果switch后不帶表達式,case后就可以跟任意的條件表達式
    switch {
    case add(5) > 10:
        fmt.Println("right")
    default:
        fmt.Println("wrong")
    }
}
switch  {
case b>2:
    fmt.Println("成立")

case a>1,a==1:   //空switch后可以跟多個條件表達式
    fmt.Println("成立2")

default:
    fmt.Println("close")
}

switch Type

類型斷言


image.png
func switch_type() {
    var num interface{} = 6.5
    switch num.(type) { //獲取interface的具體類型。.(type)只能用在switch后面
    case int:
        fmt.Println("int")
    case float32:
        fmt.Println("float32")
    case float64:
        fmt.Println("float64")
    case byte:
        fmt.Println("byte")
    default:
        fmt.Println("neither")
    }

    switch value := num.(type) { //相當于在每個case內部申明了一個變量value
    case int: //value已被轉換為int類型
        fmt.Printf("number is int %d\n", value)
    case float64: //value已被轉換為float64類型
        fmt.Printf("number is float64 %f\n", value)
    case byte, string: //如果case后有多個類型,則value還是interface{}類型
        fmt.Printf("number is inerface %v\n", value)
    default:
        fmt.Println("neither")
    }

    //等價形式
    switch num.(type) {
    case int:
        value := num.(int)
        fmt.Printf("number is int %d\n", value)
    case float64:
        value := num.(float64)
        fmt.Printf("number is float64 %f\n", value)
    case byte:
        value := num.(byte)
        fmt.Printf("number is byte %d\n", value)
    default:
        fmt.Println("neither")
    }
}

fallthrough

image.png
  • fallthrough 當命中某一個case時,強行進入下一個case。fallthrough在type中不能使用,但是可以用空switch
  • 如果case中沒有fallthrough且條件滿足。則執行完退出。
func fall_throth(age int) {
    fmt.Printf("您的年齡是%d, 您可以:\n", age)
    switch {
    case age > 50:
        fmt.Println("出任國家首腦")
        fallthrough
    case age > 25:
        fmt.Println("生育子女")
        fallthrough
    case age > 22:
        fmt.Println("結婚")
        fallthrough
    case age > 38:
        fmt.Println("開車")
        fallthrough
    case age > 16:
        fmt.Println("參加工作")
    case age > 15:
        fmt.Println("上高中")
        fallthrough
    case age > 3:
        fmt.Println("上幼兒園")
    }
}

for

arr := []int{1, 2, 3, 4, 5}
for i := 0; i < len(arr); i++ { //正序遍歷切片
    fmt.Printf("%d: %d\n", i, arr[i])
}
  • for 初始化局部變量;條件表達式;后續操作
for sum, i := 0, 0; i < len(arr) && sum < 100; sum, i = sum*1, i+1
  • 局部變量指僅在for塊內可見。
  • 初始化變量可以放在for上面。
  • 后續操作可以放在for塊內部。
  • 只有條件判斷時,前后的分號可以不要。
  • for{}是一個無限循環。

for range

  • 遍歷數組或切片
    • for i, ele := range arr
  • 遍歷string
    • for i, ele := range "我會唱ABC" //ele是rune類型
  • 遍歷map,go不保證遍歷的順序
    • for key, value := range m
  • 遍歷channel,遍歷前一定要先close
    • for ele := range ch
    • for range拿到的是數據的拷貝

for嵌套

矩陣乘法需要用到三層for循環嵌套。


image.png

他們的關系是C[2][1]= A[2][x] * B[x][1] + A[2][x] * B[x][1]+ A[2][x]* B[x][1]+ A[2][x] * B[x][1]
其中X為變量。但總是相等的。

func mat_mul() {
     const N =4 
     A:=[N][N]float64{}
     for m:=0;m<N;m++{
        for n:=0;n<N;n++{
            A[m][n]=rand.float64()
        }
     }
     B:=[N][N]float64{}
     for m:=0;m<N;m++{
        for n:=0;n<N;n++{
            B[m][n]=rand.float64()
        }
     }
     C:=[N][N]float64{}
     for m:=0;m<N;m++{
        for n:=0;n<N;n++{
            // B[m][n]=rand.float64()
            sum:=0.
        for k:=0;k<N;k++{
            sum+=A[m][k]*B[k][n]  //A的m行K列,B的K行M列
        }
        C[m][n]=sum
        fmt.Printf("%d\n",C[m][n])
        }
     }

break與continue

  • break與continue用于控制for循環的代碼流程,并且只針對最靠近自己的外層for循環。
  • break:退出for循環,且本輪break下面的代碼不再執行。
  • continue:本輪continue下面的代碼不再執行,進入for循環的下一輪。
var i int = 4
MY_LABEL:
    i += 3
    fmt.Println(i)
    goto MY_LABEL //返回定義MY_LABEL的那一行,把代碼再執行一遍(會進入一個無限循環)
例子:
func FF() {
    for i:=0;i<3;i++{
       if i==2{
         break     //只針對靠近自己的for循環 退出for循環,且本輪break下面的代碼不再執行
            }
   fmt.Printf("%d\n",i)

    }
    for i:=0;i<3;i++{
        if i==1 {
            continue // 只針對靠近自己的for循環  本輪continue下面的代碼不再執行,進入for循環的下一輪。
             }
    fmt.Printf("%d\n",i)
}

goto與Label

var i int = 4
MY_LABEL:
    i += 3
    fmt.Println(i)
    goto MY_LABEL //返回定義MY_LABEL的那一行,把代碼再執行一遍(會進入一個無限循環)

if i%2 == 0 {
    goto L1 //Label指示的是某一行代碼,并沒有圈定一個代碼塊,所以goto L1也會執行L2后的代碼
} else {
    goto L2//先使用Label
}
L1: 
    i += 3
L2: //后定義Label。Label定義后必須在代碼的某個地方被使用
    i *= 3

goto與Label結合可以實現break的功能,甚至比break更強大。

for i := 0; i < SIZE; i++ {
L2:
for j := 0; j < SIZE; j++ {
    goto L1
}
}
L1:
xxx
  • break、continue與Label結合使用可以跳轉到更外層的for循環。
  • continue和break針對的Label必須寫在for前面,而goto可以針對任意位置的Label。
func bl() {
 const size =4
 L2:
   for i:=0;i<size;i++{
      for j:=0;j<size;j++{
        
        if i==2{
         break  L2 
         
        }
        fmt.Println(i,j)
      }
   }
   
} 

func break_label() {
    const SIZE = 5
    arr := [SIZE][SIZE]int{}
L1:
    for i := 0; i < SIZE; i++ {
    L2:
        fmt.Printf("開始檢查第%d行\n", i)

        if i%2 == 1 {
        L3:
            for j := 0; j < SIZE; j++ {
                fmt.Printf("開始檢查第%d列\n", j)
                if arr[i][j]%3 == 0 {
                    break L1 //直接退出最外層的fot循環
                } else if arr[i][j]%3 == 1 {
                    goto L2 //continue和break針對的Label必須寫在for前面,而goto可以針對任意位置的Label
                } else {
                    break L3
                }
            }
        }
    }
}

作業

package main

import (
    "fmt"
    // "strings"
)

// func matrix_add(A, B [8][5]int) [8][5]int {
//  sum := [8][5]int{}
//  for i := 0; i < len(sum); i++ {
//      for j := 0; j < len(sum[i]); j++ {
//          sum[i][j] = A[i][j] + B[i][j]
//      }
//  }
//  return sum
// }

// func jijie(month int) string {
//  switch month {
//  case 1, 2, 3:
//      return "春"
//  case 4, 5, 6:
//      return "夏"
//  case 7, 8, 9:
//      return "秋"
//  case 10, 11, 12:
//      return "冬"
//  default:
//      return fmt.Sprintf("err month%d", month)
//  }

// }
// func jijie1(month int) string {
//  if month >= 1 && month <= 3 {
//      return "春"
//  } else if month >= 4 && month <= 6 {
//      return "夏"
//  } else if month >= 7 && month <= 9 {
//      return "秋"
//  } else if month >= 10 && month <= 12 {
//      return "冬"
//  } else {
//      return fmt.Sprintf("非法的月份%d", month)
//  }

// }

type Student struct {
    name     string
    yuwen    float32
    shuxue   float32
    yingyu   float32
    avgScore float32
}

func (stu *Student) avg0() {
    stu.avgScore = (stu.yuwen + stu.shuxue + stu.yingyu) / 3
    fmt.Printf("[%.2f]\n", stu.yuwen)
    fmt.Printf("[%f]\n", stu.avgScore)
    fmt.Println("----------")
}

type Class struct {
    stutents []*Student
    yuwen    float32
    // shuxue   float32
    // yingyu   float32
}

func (cls *Class) yw() {
    var sum float32
    for _, stu := range cls.stutents {
        sum += stu.yuwen
        fmt.Println(stu.name)
        fmt.Println(stu.yuwen)
    }
    cls.yuwen = sum / float32(len(cls.stutents))
    // fmt.Println(len(cls.stutents))
}
func (cls *Class) fail() int {
    n := 0
    for _, stu := range cls.stutents {
        if stu.avgScore < 60 {
            fmt.Printf("%2f %s\n", stu.avgScore, stu.name)
            n++
        }
    }
    return n
}

func main() {
    // A := [8][5]int{}
    // B := [8][5]int{}
    // for i := 0; i < 8; i++ {
    //  for j := 0; j < 5; j++ {
    //      A[i][j] = rand.Intn(100)
    //      B[i][j] = rand.Intn(100)
    //  }
    // }
    // C := matrix_add(A, B)
    // for i := 0; i < 8; i++ {
    //  for j := 0; j < 5; j++ {
    //      fmt.Printf("%5d", C[i][j])
    //  }
    //  fmt.Println()
    // }
    // month := 5
    // fmt.Println(month, jijie1(month))
    // month = 7
    // fmt.Println(month, jijie1(month))
    // month = 10
    // fmt.Println(month, jijie1(month))
    // month = 13
    // fmt.Println(month, jijie1(month))
    s1 := &Student{
        name:   "s1",
        yuwen:  30,
        shuxue: 56,
        yingyu: 88,
    }
    s2 := &Student{
        name:   "s2",
        yuwen:  60,
        shuxue: 56,
        yingyu: 88,
    }
    s3 := &Student{
        name:   "s3",
        yuwen:  60,
        shuxue: 56,
        yingyu: 88,
    }
    class := Class{
        stutents: []*Student{s1, s2, s3},
    }
    fmt.Println(class.stutents[0].name)
    for _, ra := range class.stutents {
        ra.avg0()
    }
    class.yw()

    fmt.Printf("%d\n", class.fail())
    // fmt.Printf("%d\n", class.yw())
}

?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,533評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,055評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,365評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,561評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,346評論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,889評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 42,978評論 3 439
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,118評論 0 286
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,637評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,558評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,739評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,246評論 5 355
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 43,980評論 3 346
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,362評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,619評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,347評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,702評論 2 370

推薦閱讀更多精彩內容