Skip to content

Latest commit

 

History

History
58 lines (52 loc) · 949 Bytes

4.md

File metadata and controls

58 lines (52 loc) · 949 Bytes

条件语句

if

  • if的条件里不需要括号
  • if的条件里可以定义变量

switch

  • switch会自动break,除非使用fallthrough
  • switch后可以没有表达式
package main

import (
	"fmt"
	"io/ioutil"
)

func grade(score int) string {
	g := ""
	switch {
	case score < 0 || score > 100:
		panic(fmt.Sprintf(
			"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
}

func main() {
	// If "abc.txt" is not found,
	// please check what current directory is,
	// and change filename accordingly.
	const filename = "abc.txt"
	if contents, err := ioutil.ReadFile(filename); err != nil {
		fmt.Println(err)
	} else {
		fmt.Printf("%s\n", contents)
	}

	fmt.Println(
		grade(0),
		grade(59),
		grade(60),
		grade(82),
		grade(99),
		grade(100),
		// Uncomment to see it panics.
		// grade(-3),
	)
}