返回整体状态的 Go 函数

该代码执行以下操作:

  1. 获取安装状态数组并提供一些整体状态,即字符串值

  2. 循环在阵列上,如果其中一个安装条目是错误的,则所有安装都视为错误并返回overallstatus=error

  3. 如果一个正在运行overallstatus=running

  4. 否则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

}


慕田峪9158850
浏览 102回答 1
1回答

www说

是的,它可以简化和缩短: 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 }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go