GO语言-基础语法:条件判断
生活随笔
收集整理的這篇文章主要介紹了
GO语言-基础语法:条件判断
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1. IF判斷(aa.txt內(nèi)容:asdfgh。bb.txt內(nèi)容:12345)
package mainimport ("io/ioutil""fmt" )func main() {const filename1, filename2 = "aa.txt", "bb.txt"contents, err := ioutil.ReadFile(filename1) if err != nil {fmt.Println(err)} else{fmt.Printf("%s\n", contents)}fmt.Printf("%s\n", contents) //打印出filename1的內(nèi)容if contents, err := ioutil.ReadFile(filename2);err != nil {fmt.Println(err)} else {fmt.Printf("%s\n", contents)}fmt.Printf("%s\n", contents) //還是打印出filename1的內(nèi)容,filename2在if條件內(nèi),跳出IF之后失效(IP內(nèi)部賦值,只對本次IF有效) }打印結(jié)果;
asdfgh asdfgh 12345 asdfgh [Finished in 0.6s]?2. switch
package mainimport ("fmt" )//定義函數(shù):grade:函數(shù)名。score:傳入的變量,類型是int。string:函數(shù)返回的類型。 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"default://在本次switch中可以不寫default,因?yàn)榈谝粋€case已經(jīng)判斷了所以的異常 panic(fmt.Sprintf("Wrong score: %d", score))}return g }func main() {fmt.Println(grade(50),grade(60),grade(90),grade(10)) }打印結(jié)果:
F C A F [Finished in 0.7s]?
轉(zhuǎn)載于:https://www.cnblogs.com/vijayfly/p/9474847.html
總結(jié)
以上是生活随笔為你收集整理的GO语言-基础语法:条件判断的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Intellij IDEA中maven更
- 下一篇: @PostContruct注解的使用