go 流程控制

一、什么是流程控制?

 

二、If/else分支判断

2.1转译

package main

import (
	"fmt"
	"strconv"
)

func main ( ) {
	var str string
	fmt.Scanf("%s", &str)

	number, err := strconv.Atoi(str)
	if err != nil {
		fmt.Println("convert failed err:",err)
		return
	}

	fmt.Println(number)
}

  

三、switch case语句(开关)

package main

import "fmt"

func main() {
	var a int = 0

	switch a {
	case 0:
		fmt.Println("is 0")
		fallthrough //继续往下一个分支走
	case 10:
		fmt.Println("is 10")
	default:
		fmt.Println("is no")
	}
}
输出:

E:\project>main.exe
is 0
is 10

三、for 语句

写法1:for 初始化语句;条件判断;变量修改

 

3.1for-range结构

for 指针,值  := range 传入值{  }

package main

import "fmt"

func main(){
	str := "Go is a beautiful language!"
	fmt.Printf("The length of str is: %d\n", len(str))
	for pos, char := range str {
		fmt.Printf("Character on position %d is: %c \n",pos, char)
	}
}
//输出值

The length of str is: 27
Character on position 0 is: G
Character on position 1 is: o
Character on position 2 is:
Character on position 3 is: i
Character on position 4 is: s
Character on position 5 is:
Character on position 6 is: a
Character on position 7 is:
Character on position 8 is: b
Character on position 9 is: e
Character on position 10 is: a
Character on position 11 is: u
Character on position 12 is: t
Character on position 13 is: i
Character on position 14 is: f
Character on position 15 is: u
Character on position 16 is: l
Character on position 17 is:
Character on position 18 is: l
Character on position 19 is: a
Character on position 20 is: n
Character on position 21 is: g
Character on position 22 is: u
Character on position 23 is: a
Character on position 24 is: g
Character on position 25 is: e
Character on position 26 is: !

  

 四、laber与goto

做标记使用

 

posted @ 2018-07-24 23:54  liubiaos  阅读(113)  评论(0)    收藏  举报