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
}

  

posted @ 2019-03-22 09:57  幸运使者  阅读(301)  评论(0编辑  收藏  举报