Go语言学习笔记二--判断语言

if语句

  • if语言demo,可以看出 if语句后条件不需要加()
/**
if语言demo,可以看出 if语句后条件不需要加()
*/
func ifDemo() {

	const filename = "adc.txt"

	bytes, e := ioutil.ReadFile(filename) //golang返回值可以是多个,需要多个值来接收

	if e != nil {
		fmt.Println(e)
	} else {
		fmt.Printf("%s\n", bytes)
	}

	if file, i := ioutil.ReadFile(filename); i != nil { //另一种写法,if语句中是可以跟多个语句,必须使用;分号隔开,条件中可以赋值
		fmt.Println(e)
	} else {
		fmt.Printf("%s/n", file)
	}

}

switch用法

  • switch后可以没有表达式
  • switch用法,不需要添加 break,会自动返回

/**
switch用法,不需要添加 break,会自动返回
switch后可以没有表达式
*/
func switchDemo() {
	fmt.Print(grade(81), grade(20), grade(100), grade(90))
}

func grade(soure int) string {

	g := ""
	switch {
	case soure < 60:
		g = "F"
	case soure < 80:
		g = "D"
	case soure < 90:
		g = "B"
	case soure <= 100:
		g =
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值