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

    做标记使用

  • 相关阅读:
    React 生命周期
    css 多行文本以...代替
    微信JSSDK配置文件说明
    zepto阻止事件冒泡
    PHP 图片处理PNG颜色丢失
    React 学习笔记(一)
    webpack webpack-dev-server使用指南
    为什么需要使用模块打包工具?
    如何实现微信公户绑定公众号业务
    iOS 手势
  • 原文地址:https://www.cnblogs.com/liubiaos/p/9363462.html
Copyright © 2011-2022 走看看