zoukankan      html  css  js  c++  java
  • 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
    ", len(str))
    	for pos, char := range str {
    		fmt.Printf("Character on position %d is: %c 
    ",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

    做标记使用

  • 相关阅读:
    一个合格的程序员应该读过哪些书
    一个程序员如何快速赚到一百万?
    如何创造财富?硅谷创业之父 Paul Graham 《黑客与画家》思维导图
    java 入门书籍(java7)
    活动预售和预热的目的
    活动策划
    店铺费率把控
    如何通过店铺数据分析店铺异常原因?
    刷单三大目的?如何安全、低成本、高效完成刷单
    活动报名技巧之——天天特卖
  • 原文地址:https://www.cnblogs.com/liubiaos/p/9363462.html
Copyright © 2011-2022 走看看