Before optimization: nest if, check whether there are Chinese field values, otherwise search for English values, and return the default value if none of them is not available.
if ["zh-CN"] == "" { city = ["en"] if ["en"] == "" { city = ["en"] } } else { city = "Unknown Region" }
After optimization: horizontal if...else replaces nested if
if name, ok := ["zh-CN"]; ok { city = name } else if city, ok = ["en"]; ok { city = ["en"] } else { city = "Unknown Region" }
Knowledge point: using gook
Mode, querymap
Is the value of the object more concise and safe?
Further optimization, remove else
This is very useful when else's processing logic has clear values
city = "Unknown Region" if name, ok := ["zh-CN"]; ok { city = name } else if name, ok = ["en"]; ok { city = name }
Use switch instead of multiple if branches: give the corresponding level according to the score segment of the score
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 }
After optimization: Make the code more concise and avoid multiple nestedif-else
Sentence. At the same time, useswitch
Statements can also handle the default situation, that is,score
When no branch conditions are met, the default return is "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 }
Error check
First looketcd
Error handling on github official websiteexample:
resp, err := (ctx, "", "") if err != nil { switch err { case : ("ctx is canceled by another routine: %v", err) case : ("ctx is attached with a deadline is exceeded: %v", err) case : ("client-side error: %v", err) default: ("bad cluster endpoints, which are not etcd servers: %v", err) } }
We can further optimize and usego
The package comes witherrors
Perform more robust checks on error types, but the requirements for developers are high. The correct error type needs to be predefined to ensure correct comparison of errors
resp, err := (ctx, "", "") if err2 != nil { switch { case (err, ): ("ctx is canceled by another routine: %v", err) case (err, ): ("ctx is attached with a deadline is exceeded: %v", err) case (err, ): ("client-side error: %v", err) default: ("bad cluster endpoints, which are not etcd servers: %v", err) } }
This is the end of this article about multiple if code optimization tips in Golang. For more related go if optimization content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!