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

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

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

Go
HUH函数 2023-08-07 14:48:41
美化这个函数的最 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",}
查看完整描述

1 回答

?
犯罪嫌疑人X

TA贡献2080条经验 获得超4个赞

您需要正确的抽象级别:


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

  }

}


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

添加回答

举报

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