• golang 条件语句if,switch


    package main
    
    import (
    	"fmt"
    	"io/ioutil"
    )
    
    func main() {
    	const filename = "abc.txt"
    
    	if  concents, err := ioutil.ReadFile(filename); err != nil {
    		fmt.Println(err)
    	} else {
    		fmt.Printf("%s", concents)
    	}
    
    	//concents, err := ioutil.ReadFile(filename)
    	//if err != nil {
    	//	fmt.Println(err)
    	//} else {
    	//	fmt.Printf("%s", concents)
    	//}
    }
    

    1.if的条件里是可以赋值

    2.if条件里赋值的变量作用域就在这个if语句里

    switch语句:

    不需要break,也可以直接switch多个条件

    func grade(score int) string {
    	g := ""
    	switch  {
    	case score < 0 || score > 100:
    		panic(fmt.Sprint(
    			"Wrong score: %d", score))
    	case score < 60:
    		g = "F"
    	case score < 80:
    		g = "C"
    	case score < 90:
    		g = "B"
    	case score <= 100:
    		g = "A"
    	}
    	return g
    }
    

      

  • 相关阅读:
    [转载]qemu-kvm安装配置
    Hadoop通过c语言API访问hdfs
    hadoop和hdfs环境搭建
    OpenCV installation for Ubuntu 12.04
    homework-01
    linux命令2
    压缩tar
    anaconda 安装opencv
    anconda安装第三方库
    开源代码
  • 原文地址:https://www.cnblogs.com/xingyunshizhe/p/10576432.html
Copyright © 2020-2023  润新知