Golang中多個if代碼優(yōu)化小技巧
沒優(yōu)化之前: 嵌套if, 檢查是否有中文字段值, 否則去查找英文值, 都沒有則返回默認值
if record.City.Names["zh-CN"] == "" { city = record.City.Names["en"] if record.City.Names["en"] == "" { city = record.City.Names["en"] } } else { city = "未知地域" }
優(yōu)化后: 平級if...else代替嵌套if
if name, ok := record.City.Names["zh-CN"]; ok { city = name } else if city, ok = record.City.Names["en"]; ok { city = record.City.Names["en"] } else { city = "未知地域" }
知識點: 利用go的ok
模式,對查詢map
對象的值是否存在更加簡潔和安全
更進一步的優(yōu)化, 去掉else
這對else的處理邏輯有明確的值時非常有用
city = "未知地域" if name, ok := record.City.Names["zh-CN"]; ok { city = name } else if name, ok = record.City.Names["en"]; ok { city = name }
使用switch代替多個if分支: 根據(jù)成績的分數(shù)段給出對應(yīng)的等級
func calculateGrade(score int) string { var grade string if score >= 90 && score <= 100 { grade = "A" } else if score >= 80 && score < 90 { grade = "B" } else if score >= 70 && score < 80 { grade = "C" } else if score >= 60 && score < 70 { grade = "D" } else if score >= 0 && score < 60 { grade = "F" } else { grade = "Invalid Score" } return grade }
優(yōu)化后: 使代碼更加簡潔,并且避免了多個嵌套的 if-else
語句。同時,使用 switch
語句還能夠處理默認情況,即當 score
不滿足任何一個分支條件時,默認返回 "Invalid Score"
func calculateGrade(score int) string { var grade string switch { case score >= 90 && score <= 100: grade = "A" case score >= 80 && score < 90: grade = "B" case score >= 70 && score < 80: grade = "C" case score >= 60 && score < 70: grade = "D" case score >= 0 && score < 60: grade = "F" default: grade = "Invalid Score" } return grade }
錯誤檢查
首先看etcd
github官網(wǎng)的錯誤處理例子:
resp, err := cli.Put(ctx, "", "") if err != nil { switch err { case context.Canceled: log.Fatalf("ctx is canceled by another routine: %v", err) case context.DeadlineExceeded: log.Fatalf("ctx is attached with a deadline is exceeded: %v", err) case rpctypes.ErrEmptyKey: log.Fatalf("client-side error: %v", err) default: log.Fatalf("bad cluster endpoints, which are not etcd servers: %v", err) } }
我們可以進一步優(yōu)化, 使用go
自帶的包errors
對錯誤類型進行更加健壯的檢查, 但是對開發(fā)者的要求較高, 需要預(yù)定義正確的錯誤類型, 來保證正確的比較錯誤
resp, err := cli.Put(ctx, "", "") if err2 != nil { switch { case errors.Is(err, context.Canceled): log.Fatalf("ctx is canceled by another routine: %v", err) case errors.Is(err, context.DeadlineExceeded): log.Fatalf("ctx is attached with a deadline is exceeded: %v", err) case errors.Is(err, rpctypes.ErrEmptyKey): log.Fatalf("client-side error: %v", err) default: log.Fatalf("bad cluster endpoints, which are not etcd servers: %v", err) } }
到此這篇關(guān)于Golang中多個if代碼優(yōu)化小技巧的文章就介紹到這了,更多相關(guān)go if優(yōu)化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go?處理大數(shù)組使用?for?range?和?for?循環(huán)的區(qū)別
這篇文章主要介紹了Go處理大數(shù)組使用for?range和for循環(huán)的區(qū)別,對于遍歷大數(shù)組而言,for循環(huán)能比for?range循環(huán)更高效與穩(wěn)定,這一點在數(shù)組元素為結(jié)構(gòu)體類型更加明顯,下文具體分析感興趣得小伙伴可以參考一下2022-05-05Go1.20最新資訊go?arena手動管理內(nèi)存鴿了
由于過于繁雜,Go?核心團隊成員@Ian?Lance?Taylor,也表態(tài):目前尚未做出任何決定,也不可能在短期內(nèi)做出任何決定,可以認為這個提案基本鴿了,今天這篇文章就是給大家同步目前的情況2023-11-11Go語言實現(xiàn)Base64、Base58編碼與解碼
本文主要介紹了Base64、Base58編碼與解碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07