「GoCN酷Go推荐」基于泛型的 Golang lodash 库 — samber/lo
Posted GoCN
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了「GoCN酷Go推荐」基于泛型的 Golang lodash 库 — samber/lo相关的知识,希望对你有一定的参考价值。
是一个一致性、模块化、高性能的 javascript 实用工具库,其通过降低 array、number、objects、string 等等的使用难度让 JavaScript 变得更简单。并且其不需要引入其他第三方依赖。
我们可以直接调用其中封装好的方法,比如数组去重,防抖函数等等,简化很多代码。
比如去重:
这样一套适配多种数据类型的完整的工具库是非常不容易的。有一些开源库通过其他方式实现了部分功能,大致有三种方案:
纯手撸 - 毫无疑问,这种方式是最不优雅的,需要对每种类型进行开发,需要做很多无聊的工作。 代码生成 - 通过脚本辅助生成针对不同类型的工具函数,比如 go-dash/slice。 使用反射 - 这种方式可以实现目的,但是反射会带来较大复杂度和造成运行时性能的下降。go-funk 和robpike/filter都是通过该种方式实现的工具库。 somber/lo
是一个基于 Golang 泛型实现的的 lodash 风格工具库,比较好的避免了上面的问题。
somber/lo
包含了非常多的方法,主要可以划分为以下几类:
slice 辅助方法 map 辅助方法 tuples 辅助方法 多个集合之间计算辅助方法 搜索查询辅助方法 其他函数式编程辅助方法等 以切片去重举例:
基于泛型包装了非常多的工具方法,可以大大节省我们的开发时间,避免重复开发,提升效率。但是该库开源至今才两周,可能会有一些问题缺陷存在其中,线上使用还需要谨慎一些。
Reference
Go 1.18 is released! - The Go Programming Language
The benefits of using Lodash in the Go language without reflection (freecodecamp.org)
samber/lo:
GoCN酷Go推荐Go 系统监控利器-gopsutil
简介
什么是 gopsutil?要说gopsutil就不得不先了解 psutil,顾名思义,psutil = process and system utilities, 它是 Python 的跨平台库,能够轻松实现获取系统运行的进程和系统利用率,包括 CPU、内存、磁盘、网络等信息。而gopsutil就是psutil的 Golang 移植版。
为什么用 gopsutil?和直接使用 syscall 调用对应的系统方法相比,gopsutil 为我们屏蔽了各个系统间的差异,可移植性非常强。
快速上手
安装:
go get github.com/shirou/gopsutil
使用:
package main
import (
"fmt"
"github.com/shirou/gopsutil/cpu"
"time"
)
func main() {
info, _ := cpu.Info()
per, _ := cpu.Percent(1 * time.Second, true)
fmt.Printf("CPU Percent: %f\n", per)
fmt.Println(info)
}
输出:
CPU Percent: [4.040404 4.000000 5.050505 6.930693]
[{"cpu":0,"vendorId":"AuthenticAMD","family":"23","model":"49","stepping":0,"physicalId":"0","coreId":"0","cores":1,"modelName":"AMD EPYC 7K62 48-Core Processor","mhz":2595.124,"cacheSize":512,"flags":["fpu","vme","de","pse","tsc","msr","pae","mce","cx8","apic","sep","mtrr","pge","mca","cmov","pat","pse36","clflush","mmx","fxsr","sse","sse2","ht","syscall","nx","mmxext","fxsr_opt","pdpe1gb","rdtscp","lm","rep_good","nopl","cpuid","extd_apicid","tsc_known_freq","pni","pclmulqdq","ssse3","fma","cx16","sse4_1","sse4_2","x2apic","movbe","popcnt","aes","xsave","avx","f16c","rdrand","hypervisor","lahf_lm","cmp_legacy","cr8_legacy","abm","sse4a","misalignsse","3dnowprefetch","osvw","topoext","ibpb","vmmcall","fsgsbase","bmi1","avx2","smep","bmi2","rdseed","adx","smap","clflushopt","sha_ni","xsaveopt","xsavec","xgetbv1","arat"],"microcode":"0x1000065"} {"cpu":1,"vendorId":"AuthenticAMD","family":"23","model":"49","stepping":0,"physicalId":"0","coreId":"1","cores":1,"modelName":"AMD EPYC 7K62 48-Core Processor","mhz":2595.124,"cacheSize":512,"flags":["fpu","vme","de","pse","tsc","msr","pae","mce","cx8","apic","sep","mtrr","pge","mca","cmov","pat","pse36","clflush","mmx","fxsr","sse","sse2","ht","syscall","nx","mmxext","fxsr_opt","pdpe1gb","rdtscp","lm","rep_good","nopl","cpuid","extd_apicid","tsc_known_freq","pni","pclmulqdq","ssse3","fma","cx16","sse4_1","sse4_2","x2apic","movbe","popcnt","aes","xsave","avx","f16c","rdrand","hypervisor","lahf_lm","cmp_legacy","cr8_legacy","abm","sse4a","misalignsse","3dnowprefetch","osvw","topoext","ibpb","vmmcall","fsgsbase","bmi1","avx2","smep","bmi2","rdseed","adx","smap","clflushopt","sha_ni","xsaveopt","xsavec","xgetbv1","arat"],"microcode":"0x1000065"} {"cpu":2,"vendorId":"AuthenticAMD","family":"23","model":"49","stepping":0,"physicalId":"0","coreId":"2","cores":1,"modelName":"AMD EPYC 7K62 48-Core Processor","mhz":2595.124,"cacheSize":512,"flags":["fpu","vme","de","pse","tsc","msr","pae","mce","cx8","apic","sep","mtrr","pge","mca","cmov","pat","pse36","clflush","mmx","fxsr","sse","sse2","ht","syscall","nx","mmxext","fxsr_opt","pdpe1gb","rdtscp","lm","rep_good","nopl","cpuid","extd_apicid","tsc_known_freq","pni","pclmulqdq","ssse3","fma","cx16","sse4_1","sse4_2","x2apic","movbe","popcnt","aes","xsave","avx","f16c","rdrand","hypervisor","lahf_lm","cmp_legacy","cr8_legacy","abm","sse4a","misalignsse","3dnowprefetch","osvw","topoext","ibpb","vmmcall","fsgsbase","bmi1","avx2","smep","bmi2","rdseed","adx","smap","clflushopt","sha_ni","xsaveopt","xsavec","xgetbv1","arat"],"microcode":"0x1000065"} {"cpu":3,"vendorId":"AuthenticAMD","family":"23","model":"49","stepping":0,"physicalId":"0","coreId":"3","cores":1,"modelName":"AMD EPYC 7K62 48-Core Processor","mhz":2595.124,"cacheSize":512,"flags":["fpu","vme","de","pse","tsc","msr","pae","mce","cx8","apic","sep","mtrr","pge","mca","cmov","pat","pse36","clflush","mmx","fxsr","sse","sse2","ht","syscall","nx","mmxext","fxsr_opt","pdpe1gb","rdtscp","lm","rep_good","nopl","cpuid","extd_apicid","tsc_known_freq","pni","pclmulqdq","ssse3","fma","cx16","sse4_1","sse4_2","x2apic","movbe","popcnt","aes","xsave","avx","f16c","rdrand","hypervisor","lahf_lm","cmp_legacy","cr8_legacy","abm","sse4a","misalignsse","3dnowprefetch","osvw","topoext","ibpb","vmmcall","fsgsbase","bmi1","avx2","smep","bmi2","rdseed","adx","smap","clflushopt","sha_ni","xsaveopt","xsavec","xgetbv1","arat"],"microcode":"0x1000065"}]
分工明确
gopsutil 将不同的功能划分到不同的子包中:主要为 cpu,disk,docker,host,mem,net,process,winservices 这几个。想要使用对应的功能,要导入对应的子包。例如,上面代码中,我们要获取 CPU 信息,导入的是 cpu 子包。上述样例中,我们获取到了每个 cpu 的占用率和所有 cpu 的详细信息。
闲言
最近正在写一个 Golang 实现性能监控的 demo,之后还会写这方面的介绍或者对比。
参考文档
https://github.com/shirou/gopsutil
还想了解更多吗?
更多请查看:https://github.com/shirou/gopsutil
《酷Go推荐》招募:
各位Gopher同学,最近我们社区打算推出一个类似GoCN每日新闻的新栏目《酷Go推荐》,主要是每周推荐一个库或者好的项目,然后写一点这个库使用方法或者优点之类的,这样可以真正的帮助到大家能够学习到
新的库,并且知道怎么用。
扫码也可以加入 GoCN 的大家族哟~
Gopher China2021大会日程详情来了!
点击下方「阅读原文」即可报名参加大会
以上是关于「GoCN酷Go推荐」基于泛型的 Golang lodash 库 — samber/lo的主要内容,如果未能解决你的问题,请参考以下文章