🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] > https://github.com/samber/lo ## 概述 安装 ``` import ( "github.com/samber/lo" lop "github.com/samber/lo/parallel" ) ``` - lo 为非并发的执行 - lop 表示并发执行 ## 函数 Supported helpers for slices: * [Filter](https://github.com/samber/lo#filter) * [Map](https://github.com/samber/lo#map) * [FilterMap](https://github.com/samber/lo#filtermap) * [FlatMap](https://github.com/samber/lo#flatmap) * [Reduce](https://github.com/samber/lo#reduce) * [ReduceRight](https://github.com/samber/lo#reduceright) * [ForEach](https://github.com/samber/lo#foreach) * [Times](https://github.com/samber/lo#times) * [Uniq](https://github.com/samber/lo#uniq) * [UniqBy](https://github.com/samber/lo#uniqby) * [GroupBy](https://github.com/samber/lo#groupby) * [Chunk](https://github.com/samber/lo#chunk) * [PartitionBy](https://github.com/samber/lo#partitionby) * [Flatten](https://github.com/samber/lo#flatten) * [Interleave](https://github.com/samber/lo#interleave) * [Shuffle](https://github.com/samber/lo#shuffle) * [Reverse](https://github.com/samber/lo#reverse) * [Fill](https://github.com/samber/lo#fill) * [Repeat](https://github.com/samber/lo#repeat) * [RepeatBy](https://github.com/samber/lo#repeatby) * [KeyBy](https://github.com/samber/lo#keyby) * [Associate / SliceToMap](https://github.com/samber/lo#associate-alias-slicetomap) * [Drop](https://github.com/samber/lo#drop) * [DropRight](https://github.com/samber/lo#dropright) * [DropWhile](https://github.com/samber/lo#dropwhile) * [DropRightWhile](https://github.com/samber/lo#droprightwhile) * [Reject](https://github.com/samber/lo#reject) * [Count](https://github.com/samber/lo#count) * [CountBy](https://github.com/samber/lo#countby) * [CountValues](https://github.com/samber/lo#countvalues) * [CountValuesBy](https://github.com/samber/lo#countvaluesby) * [Subset](https://github.com/samber/lo#subset) * [Slice](https://github.com/samber/lo#slice) * [Replace](https://github.com/samber/lo#replace) * [ReplaceAll](https://github.com/samber/lo#replaceall) * [Compact](https://github.com/samber/lo#compact) * [IsSorted](https://github.com/samber/lo#issorted) * [IsSortedByKey](https://github.com/samber/lo#issortedbykey) Supported helpers for maps: * [Keys](https://github.com/samber/lo#keys) * [ValueOr](https://github.com/samber/lo#valueor) * [Values](https://github.com/samber/lo#values) * [PickBy](https://github.com/samber/lo#pickby) * [PickByKeys](https://github.com/samber/lo#pickbykeys) * [PickByValues](https://github.com/samber/lo#pickbyvalues) * [OmitBy](https://github.com/samber/lo#omitby) * [OmitByKeys](https://github.com/samber/lo#omitbykeys) * [OmitByValues](https://github.com/samber/lo#omitbyvalues) * [Entries / ToPairs](https://github.com/samber/lo#entries-alias-topairs) * [FromEntries / FromPairs](https://github.com/samber/lo#fromentries-alias-frompairs) * [Invert](https://github.com/samber/lo#invert) * [Assign (merge of maps)](https://github.com/samber/lo#assign) * [MapKeys](https://github.com/samber/lo#mapkeys) * [MapValues](https://github.com/samber/lo#mapvalues) * [MapEntries](https://github.com/samber/lo#mapentries) * [MapToSlice](https://github.com/samber/lo#maptoslice) Supported math helpers: * [Range / RangeFrom / RangeWithSteps](https://github.com/samber/lo#range--rangefrom--rangewithsteps) * [Clamp](https://github.com/samber/lo#clamp) * [Sum](https://github.com/samber/lo#sum) * [SumBy](https://github.com/samber/lo#sumby) Supported helpers for strings: * [RandomString](https://github.com/samber/lo#randomstring) * [Substring](https://github.com/samber/lo#substring) * [ChunkString](https://github.com/samber/lo#chunkstring) * [RuneLength](https://github.com/samber/lo#runelength) Supported helpers for tuples: * [T2 -> T9](https://github.com/samber/lo#t2---t9) * [Unpack2 -> Unpack9](https://github.com/samber/lo#unpack2---unpack9) * [Zip2 -> Zip9](https://github.com/samber/lo#zip2---zip9) * [Unzip2 -> Unzip9](https://github.com/samber/lo#unzip2---unzip9) Supported helpers for channels: * [ChannelDispatcher](https://github.com/samber/lo#channeldispatcher) * [SliceToChannel](https://github.com/samber/lo#slicetochannel) * [Generator](https://github.com/samber/lo#generator) * [Buffer](https://github.com/samber/lo#buffer) * [BufferWithTimeout](https://github.com/samber/lo#bufferwithtimeout) * [FanIn](https://github.com/samber/lo#fanin) * [FanOut](https://github.com/samber/lo#fanout) Supported intersection helpers: * [Contains](https://github.com/samber/lo#contains) * [ContainsBy](https://github.com/samber/lo#containsby) * [Every](https://github.com/samber/lo#every) * [EveryBy](https://github.com/samber/lo#everyby) * [Some](https://github.com/samber/lo#some) * [SomeBy](https://github.com/samber/lo#someby) * [None](https://github.com/samber/lo#none) * [NoneBy](https://github.com/samber/lo#noneby) * [Intersect](https://github.com/samber/lo#intersect) * [Difference](https://github.com/samber/lo#difference) * [Union](https://github.com/samber/lo#union) * [Without](https://github.com/samber/lo#without) * [WithoutEmpty](https://github.com/samber/lo#withoutempty) Supported search helpers: * [IndexOf](https://github.com/samber/lo#indexof) * [LastIndexOf](https://github.com/samber/lo#lastindexof) * [Find](https://github.com/samber/lo#find) * [FindIndexOf](https://github.com/samber/lo#findindexof) * [FindLastIndexOf](https://github.com/samber/lo#findlastindexof) * [FindOrElse](https://github.com/samber/lo#findorelse) * [FindKey](https://github.com/samber/lo#findkey) * [FindKeyBy](https://github.com/samber/lo#findkeyby) * [FindUniques](https://github.com/samber/lo#finduniques) * [FindUniquesBy](https://github.com/samber/lo#finduniquesby) * [FindDuplicates](https://github.com/samber/lo#findduplicates) * [FindDuplicatesBy](https://github.com/samber/lo#findduplicatesby) * [Min](https://github.com/samber/lo#min) * [MinBy](https://github.com/samber/lo#minby) * [Max](https://github.com/samber/lo#max) * [MaxBy](https://github.com/samber/lo#maxby) * [Last](https://github.com/samber/lo#last) * [Nth](https://github.com/samber/lo#nth) * [Sample](https://github.com/samber/lo#sample) * [Samples](https://github.com/samber/lo#samples) Conditional helpers: * [Ternary](https://github.com/samber/lo#ternary) * [TernaryF](https://github.com/samber/lo#ternaryf) * [If / ElseIf / Else](https://github.com/samber/lo#if--elseif--else) * [Switch / Case / Default](https://github.com/samber/lo#switch--case--default) Type manipulation helpers: * [ToPtr](https://github.com/samber/lo#toptr) * [EmptyableToPtr](https://github.com/samber/lo#emptyabletoptr) * [FromPtr](https://github.com/samber/lo#fromptr) * [FromPtrOr](https://github.com/samber/lo#fromptror) * [ToSlicePtr](https://github.com/samber/lo#tosliceptr) * [ToAnySlice](https://github.com/samber/lo#toanyslice) * [FromAnySlice](https://github.com/samber/lo#fromanyslice) * [Empty](https://github.com/samber/lo#empty) * [IsEmpty](https://github.com/samber/lo#isempty) * [IsNotEmpty](https://github.com/samber/lo#isnotempty) * [Coalesce](https://github.com/samber/lo#coalesce) Function helpers: * [Partial](https://github.com/samber/lo#partial) * [Partial2 -> Partial5](https://github.com/samber/lo#partial2---partial5) Concurrency helpers: * [Attempt](https://github.com/samber/lo#attempt) * [AttemptWhile](https://github.com/samber/lo#attemptwhile) * [AttemptWithDelay](https://github.com/samber/lo#attemptwithdelay) * [AttemptWhileWithDelay](https://github.com/samber/lo#attemptwhilewithdelay) * [Debounce](https://github.com/samber/lo#debounce) * [DebounceBy](https://github.com/samber/lo#debounceby) * [Synchronize](https://github.com/samber/lo#synchronize) * [Async](https://github.com/samber/lo#async) * [Transaction](https://github.com/samber/lo#transaction) Error handling: * [Validate](https://github.com/samber/lo#validate) * [Must](https://github.com/samber/lo#must) * [Try](https://github.com/samber/lo#try) * [Try1 -> Try6](https://github.com/samber/lo#try0-6) * [TryOr](https://github.com/samber/lo#tryor) * [TryOr1 -> TryOr6](https://github.com/samber/lo#tryor0-6) * [TryCatch](https://github.com/samber/lo#trycatch) * [TryWithErrorValue](https://github.com/samber/lo#trywitherrorvalue) * [TryCatchWithErrorValue](https://github.com/samber/lo#trycatchwitherrorvalue) * [ErrorsAs](https://github.com/samber/lo#errorsas) ## 示例 ### lo 和lop 的foreach lo ``` import "github.com/samber/lo" lo.ForEach([]string{"hello", "world"}, func(x string, _ int) { println(x) }) // prints "hello\nworld\n" ``` lop ``` import lop "github.com/samber/lo/parallel" lop.ForEach([]string{"hello", "world"}, func(x string, _ int) { println(x) }) // prints "hello\nworld\n" or "world\nhello\n" ```