如何避免else在嵌套的 if/else 语句中写两次语句?
func GetPortFromEnvironment(name string, defaultPort int32) int32 {
var port int32
if env := os.Getenv("SERVICE_PORT"); env != "" {
if _port, err := strconv.ParseInt(env, 10, 32); err != nil {
port = int32(_port)
} else {
port = defaultPort
}
} else {
port = defaultPort
}
return port
}
去
catspeake
相关分类