美化丑陋的三层嵌套 for 循环

美化这个函数的最 Go 方式是什么?这就是我想出的,有点效果,但它太丑了,任何关于美化它的帮助将不胜感激。


negate如果可能的话,我们也希望能够实现此功能。我可以不使用函数文字、映射等吗?


var UsageTypes = []string{

"PHYSICAL_SIZE",

"PHYSICAL_SIZE",

"PROVISIONED_SIZE",

"SNAPSHOT_SIZE",

"LOGICAL_SIZE_PERCENTAGE",

"TOTAL_VOLUME_SIZE",

"ALLOCATED_SIZE",

"ALLOCATED_USED",

"TOTAL_LOGICAL_SIZE",

"TOTAL_LOGICAL_SIZE_PERCENTAGE",

"TOTAL_SNAPSHOT_SIZE",

"LOGICAL_OR_ALLOCATED_GREATER_SIZE",

}


var MeasuredTypes = []string{

"LIF_RECEIVED_DATA",

"ECEIVED_ERRORS",

"LIF_RECEIVED_PACKET",

"LIF_SENT_DATA",

"LIF_SENT_ERRORS",

"LIF_SENT_PACKET",

"LINK_CURRENT_STATE",

"RX_BYTES",

"RX_DISCARDS",

"RX_CRC_ERRORS",

"RX_ERRORS",

"RX_FRAMES",

"LINK_UP_TO_DOWNS",

"TX_BYTES",

"TX_DISCARDS",

"TX_ERRORS",

"TX_HW_ERRORS",

"TX_FRAMES",

"LOGICAL_OR_ALLOCATED_GREATER_SIZE",

"LOGICAL_SIZE",

"PHYSICAL_SIZE",

"PROVISIONED_SIZE",

"SNAPSHOT_SIZE",

"VOLUME_ONLINE",

"TOTAL_THROUGHPUT",

"LOGICAL_SIZE_PERCENTAGE",

"READ_THROUGHPUT",

"WRITE_THROUGHPUT",

"OTHER_THROUGHPUT",

"TOTAL_IOPS",

"WRITE_IOPS",

"READ_IOPS",

"OTHER_IOPS",

"AVERAGE_TOTAL_LATENCY",

"AVERAGE_WRITE_LATENCY",

"AVERAGE_READ_LATENCY",

"AVERAGE_OTHER_LATENCY",

"FILESYSTEM_READ_OPS",

"FILESYSTEM_WRITE_OPS",

"FILESYSTEM_TOTAL_OPS",

"FILESYSTEM_OTHER_OPS",

"IO_BYTES_PER_READ_OPS",

"IO_BYTES_PER_WRITE_OPS",

"IO_BYTES_PER_OTHER_OPS",

"IO_BYTES_PER_TOTAL_OPS",

"READ_IO",

"WRITE_IO",

"TOTAL_IO",

"OTHER_IO",

"ACTIVE_CONNECTIONS",

"TOTAL_VOLUME_SIZE",

"ALLOCATED_SIZE",

"ALLOCATED_USED",

"TOTAL_LOGICAL_SIZE",

"TOTAL_LOGICAL_SIZE_PERCENTAGE",

"TOTAL_SNAPSHOT_SIZE",

"ONTAP_CAPACITY_DISK_CAPACITY",

"ONTAP_CAPACITY_TOTAL_STORAGE_EFFICIENCY_RATIO",

"ONTAP_CAPACITY_TOTAL_PHYSICAL_USED",

"ONTAP_CAPACITY_SIZE_USED",

"ONTAP_CAPACITY_MEMORY",

"ONTAP_CAPACITY_AVERAGE_PROCESSOR_BUSY",

"ONTAP_CAPACITY_PEAK_PROCESSOR_BUSY",

}


HUH函数
浏览 86回答 1
1回答

犯罪嫌疑人X

您需要正确的抽象级别:func has(in string[], item string) bool {   for _,x:=range in {      if x==item {        return true      }   }   return false}func isMeasuredTypeAUsageMetric(measuredTypeIn []string) []string {  result:=[]string{}  for _,item:=range measuredTypeIn {      if has(MeasuredTypes,item) {         if has(UsageTypes,item) {            result=append(result,item)         }      } else {        ///error     }  }  return result}map[string]bool通过使用 a代替 [] 字符串作为文字可以进一步简化。var MeasuredTypes=map[string]bool{"itemInUsageTypes": true,"itemNotInUsageTypes":false,...}然后你可以这样做:usage,measured:=MeasuredTypes[item]if measured {  // It is measured type  if usage {     // It is usage type  }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go