美化丑陋的三个嵌套循环

时间:2019-11-26 00:24:52

标签: go nested-loops

美化该功能的最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",
}

func isMeasuredTypeAUsageMetric(measuredTypeIn []string) []string {
  result := []string{}
  for i, _ := range measuredTypeIn {
    var foundInBigList bool
      for j, _ := range MeasuredTypes {
        if measuredTypeIn[i] == MeasuredTypes[j] {
          foundInBigList = true
          fmt.Println("found in big list: ", measuredTypeIn[i])
          for k, _ := range UsageTypes {
            if measuredTypeIn[i] == UsageTypes[k] {
              fmt.Println("found in inner list: ", measuredTypeIn[i])
              result = append(result, measuredTypeIn[i])
            }
          }
        }
      }
      if foundInBigList == false {
        fmt.Println("not found, throw exception")
      }
    }
    return result
  }

func main() {
  measuredTypeIn := []string{"LOGICAL_SIZE_PERCENTAGE", "LOGICAL_OR_ALLOCATED_GREATER_SIZE", "BUKK", "ONTAP_CAPACITY_PEAK_PROCESSOR_BUSY",}
  fmt.Println(isMeasuredTypeAUsageMetric(measuredTypeIn))
}

1 个答案:

答案 0 :(得分:1)

您需要正确的抽象级别:

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代替文字的[]字符串来进一步简化。

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
  }
}