go语言之if语句和switch语句和循环语句

1、if语句

package main

import (
	"fmt"
	"io/ioutil"
)

func main() {
	//流程控制

	//使用常量定义一个文件名
	const filename  = "golang_1.go"

	//打开一个文件,返回两个值,一个是文件的内容,一个是错误
	con,err := ioutil.ReadFile(filename)
	//这里的nil和python中的None是一样的
	if err != nil{
		fmt.Println(err)
	}else {
		fmt.Printf("%s\n",con)
	}



}

  

2、switch语句

package main

import "fmt"

//switch结构

//go语言的switch默认匹配到一项后,就不在往下匹配了
//如果加一个fallthrough关键字,还会往下匹配
func test(score int) string {
	var g = ""
	switch  {
	case score < 0 || score >100:
		g = "输入错误"
		fallthrough
	case score < 80:
		g = "B"
	case score < 90:
		g = "A"
	case score == 100:
		g = "A++"
	}
	return g

}

func main() {
	//fmt.Println(test(101))
	//fmt.Println(test(10))
	//fmt.Println(test(70))
	//fmt.Println(test(80))
	fmt.Println(test(100))
}

  

猜你喜欢

转载自www.cnblogs.com/bainianminguo/p/10847315.html