go if for while 的使用

fileName := "a.txt"
contents ,err := ioutil.ReadFile(fileName)
if err != nil{
fmt.Println("文件不存在")
}else{
fmt.Printf("%s\n",contents)
}

fileName := "a.txt"
if contents ,err := ioutil.ReadFile(fileName);err!=nil{
 fmt.Println("文件不存在")
}else{
 fmt.Printf("%s\n",contents)
}

go 语言的switch 默认会加break
var c = 2
switch c {
case 1 :
fmt.Print(c)
case 2:
fmt.Print("hello")
case 3:
fmt.Print("world")
default:
fmt.Println("this is the end")

switch 后面可以不跟表达式

var score = 80
 switch {
case score >90 :
fmt.Print(score)
case score >80:
fmt.Print("hello")
case score >70:
fmt.Print("world")
default:
fmt.Println("this is the end")


posted on 2019-09-23 08:53  paulversion  阅读(406)  评论(0编辑  收藏  举报