返回总体状态的Go函数

huangapple go评论166阅读模式
英文:

Go function that return overall status

问题

代码的功能如下:

  1. 获取安装状态的数组,并提供一些整体状态,该状态是字符串值。
  2. 循环遍历数组,如果其中一个安装条目是错误的,则所有安装都被视为错误,并返回 overallstatus=error
  3. 如果有一个正在运行,则 overallstatus=running
  4. 否则 overallstatus=installing

我的问题是,是否有更简单/更短的方法来用Go语言编写这段代码?

func overallInstallationStatus(installStatus []apiv.Installstatus) string {
	overallStatus := ""
	for _, status := range installStatus {
		switch status.ReleaseStatus {
		case release.StatusFailed.String():
			// 如果至少有一个安装失败,我们将整体状态视为错误状态
			overallStatus = "error"
		case release.StatusDeployed.String():
			// 如果没有找到其他状态,并且至少有一个已部署的图表,则将其视为“running”
			if overallStatus == "" {
				overallStatus = "running"
			}
		default:
			// 其他所有状态都被视为“installing”
			if overallStatus != release.StatusFailed.String() {
				overallStatus = "installing"
			}
		}
	}
	return overallStatus
}
英文:

The code does the following:

  1. getting array of installation status and provide some overall status which is string value
  2. loop on the array and if there is one of the installation entry is error all the installations consider as error and return overallstatus=error
  3. if one is running the overallstatus=running
  4. otherwise overallstatus=installing

My question is if there a simpler/shorter to write it in go?

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
}



</details>


# 答案1
**得分**: 1

是的,可以简化和缩短代码:

```go
func overallInstallationStatus(installStatus []apiv.Installstatus) string {
	overallStatus := "running"
	for _, status := range installStatus {
		switch status.ReleaseStatus {
		case release.StatusFailed.String():
			// 如果至少有一个安装失败,我们认为整体状态是错误的
			return "error"
		case release.StatusDeployed.String():
			// 如果没有找到其他状态,并且至少有一个已部署的图表,我们认为它是“running”
			continue
		default:
			// 所有其他状态都被认为是“installing”
			overallStatus = "installing"
		}
	}
	return overallStatus
}

请注意,这只是对原始代码的简化和缩短,没有改变其功能。

英文:

Yes, it can be simplified and shortened:

 func overallInstallationStatus(installStatus []apiv.Installstatus) string {
 	overallStatus := &quot;running&quot;
 	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 &quot;error&quot;
 		case release.StatusDeployed.String():
 			  //If no other status was found and there is at least one deployed chart, we consider it &quot;running&quot;
			  continue
 		default:
 			  //All other statuses are considered to be &quot;installing&quot;
			  overallStatus = &quot;installing&quot;
 		}
 	}
 	return overallStatus
 }

huangapple
  • 本文由 发表于 2021年5月20日 19:18:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/67619323.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定