为了账号安全,请及时绑定邮箱和手机立即绑定

返回整体状态的 Go 函数

返回整体状态的 Go 函数

Go
慕田峪9158850 2022-08-30 21:58:47
该代码执行以下操作:获取安装状态数组并提供一些整体状态,即字符串值循环在阵列上,如果其中一个安装条目是错误的,则所有安装都视为错误并返回overallstatus=error如果一个正在运行overallstatus=running否则overallstatus=installing我的问题是,如果有更简单/更短的写法?func overallInstallationStatus(installStatus []apiv.Installstatus) string {    overallStatus := ""    for _, status := range installStatus {        switch status.ReleaseStatus {        case release.StatusFailed.String():            // If at least one installation is in failed, we consider the overallstatus to be in error state            overallStatus = "error"        case release.StatusDeployed.String():            // If no other status was found and there is at least one deployed chart, we consider it "running"            if overallStatus == "" {                overallStatus = "running"            }        default:            // All other statuses are considered to be "installing"            if overallStatus != release.StatusFailed.String() {                overallStatus = "installing"            }        }    }    return overallStatus}
查看完整描述

1 回答

?
www说

TA贡献1775条经验 获得超8个赞

是的,它可以简化和缩短:


 func overallInstallationStatus(installStatus []apiv.Installstatus) string {

    overallStatus := "running"

    for _, status := range installStatus {

        switch status.ReleaseStatus {

        case release.StatusFailed.String():

              //If at least one installation is in failed, we consider the overallstatus to be in error state 

              return "error"

        case release.StatusDeployed.String():

              //If no other status was found and there is at least one deployed chart, we consider it "running"

              continue

        default:

              //All other statuses are considered to be "installing"

              overallStatus = "installing"

        }

    }

    return overallStatus

 }


查看完整回答
反对 回复 2022-08-30
  • 1 回答
  • 0 关注
  • 101 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信