Generate samber/lo methods for struct.
- Easy Setup & Removal
- Code Generation & Zero Package Size
- Extend samber/lo
- Get samber/lo in your project.
$ go install github.com/snamiki1212/go-gen-lo@latest
$ go-gen-lo --help
# -> To ensure it was installed correctly, otherwise set up your GOPATH like `export PATH=$PATH:$(go env GOPATH)/bin`package main
type User struct {
UserID string
}
+//go:generate go-gen-lo --entity=User --slice=Users --input=user.go --output=users_lo_gen.go
type Users []User+// Code generated by "go-gen-lo"; DO NOT EDIT.
+// Based on information from https://github.com/snamiki1212/go-gen-lo
+
+package main
+
+import "github.com/samber/lo"
+
+// Find
+func (xs Users) Find(predicate func(User) bool) (User, bool) {
+ return lo.Find(xs, predicate)
+}
+ ...4. Use samber/lo from method.
package main
func main() {
us := Users{{UserID: "1"}, {UserID: "2"}, {UserID: "3"}}
// 🚀 You can use lo with slice method
u, _ := us.Find(func(entity User) bool { return entity.UserID == "2" })
fmt.Println(u) // {UserID:2}
// 🚀 You can use Extended samber/lo
u, _ = us.FindByUserID("3")
fmt.Println(u) // {UserID:3}
}Tip
Install a binary with go:generate and your team no need care about installation but just run go generate ..
+//go:generate go install github.com/snamiki1212/go-gen-lo@latest
+//go:generate go-gen-lo --entity=User --slice=Users --input=user.go --output=users_lo_gen.go
type Users []UserGenerate lo methods for struct.
Usage:
go-gen-lo [flags]
Flags:
-e, --entity string Target entity name.
ex) --entity=User
ex) --entity=*User
-x, --exclude strings Exclude lo method with regex
ex) --exclude=Filter$,Find
-h, --help help for go-gen-lo
-n, --include strings Include lo method with regex
ex) --include=Filter$,Find
-i, --input string Input file name
-o, --output string Output file name
-r, --rename strings Rename lo method with regex
ex) --rename=Find:Get
NOTE: Placeholders are available but not dollar($) but backslash(\).
ex) --rename="Filter(.*):Lo\{0}" # FilterUserID -> LoFilterUserID
-s, --slice string Target slice name.
ex) --slice=UsersGenerated example Codes.
- Common case (source)
- Exclude (source)
- Include (source)
- Rename (source)
- Recommend case (source)
- Rename to add
Lo~prefix in order to search easily. - Only pick up frequently used helper.
- Rename to add
- ✅ Support
- 🚫 Cannot Support (See issue)
- 🔨 Not supported yet (Welcome PRs)
ーNo Need
🚀🚀🚀Support List for all lo🚀🚀🚀
| samber/lo | Struct | Std | Extend |
|---|---|---|---|
| Filter | []T |
✅ | ✅ |
| Map | []T |
🚫 | ー |
| FilterMap | []T |
🚫 | ー |
| FlatMap | []T |
🚫 | ー |
| Reduce | []T |
🚫 | ー |
| ReduceRight | []T |
🚫 | ー |
| ForEach | []T |
🔨 | ー |
| ForEachWhile | []T |
🔨 | ー |
| Times | int |
ー | ー |
| Uniq | []T |
ー | ー |
| UniqBy | []T |
🚫 | ✅ |
| GroupBy | []T |
🚫 | ✅ |
| Chunk | []T |
🔨 | ー |
| PartitionBy | []T |
🚫 | ー |
| Flatten | []T |
ー | ー |
| Interleave | [][]T |
ー | ー |
| Shuffle | []T |
🔨 | ー |
| Reverse | []T |
🔨 | ー |
| Fill | []T |
🔨 | ー |
| Repeat | int |
ー | ー |
| RepeatBy | int |
ー | ー |
| KeyBy | []T |
🚫 | ✅ |
| Associate / SliceToMap | []T |
🚫 | ー |
| Drop | []T |
🔨 | ー |
| DropRight | []T |
🔨 | ー |
| DropWhile | []T |
🔨 | ー |
| DropRightWhile | []T |
🔨 | ー |
| DropByIndex | []T |
🔨 | ー |
| Reject | []T |
🔨 | 🔨 |
| RejectMap | []T |
🚫 | ー |
| FilterReject | []T |
✅ | ✅ |
| Count | []T |
ー | ー |
| CountBy | []T |
🔨 | ー |
| CountValues | []T |
ー | ー |
| CountValuesBy | []T |
🚫 | ー |
| Subset | []T |
🔨 | ー |
| Slice | []T |
🔨 | ー |
| Replace | []T |
ー | ー |
| ReplaceAll | []T |
ー | ー |
| Compact | []T |
ー | ー |
| IsSorted | []T |
ー | ー |
| IsSortedByKey | []T |
ー | ー |
| Splice | []T |
🔨 | ー |
| Keys | map[any]T |
🔨 | ー |
| UniqKeys | map[any]T |
ー | ー |
| HasKey | map[any]T |
🔨 | ー |
| ValueOr | map[any]T |
🔨 | ー |
| Values | map[any]T |
🔨 | ー |
| UniqValues | map[any]T |
ー | ー |
| PickBy | map[any]T |
🔨 | 🔨 |
| PickByKeys | map[any]T |
🔨 | ー |
| PickByValues | map[any]T |
ー | ー |
| OmitBy | map[any]T |
🔨 | ー |
| OmitByKeys | map[any]T |
🔨 | ー |
| OmitByValues | map[any]T |
ー | ー |
| Entries / ToPairs | map[any]T |
🔨 | ー |
| FromEntries / FromPairs | map[any]T |
ー | ー |
| Invert | map[any]T |
ー | ー |
| Assign (merge of maps) | map[any]T |
ー | ー |
| MapKeys | map[any]T |
ー | ー |
| MapValues | map[any]T |
ー | ー |
| MapEntries | map[any]T |
ー | ー |
| MapToSlice | map[any]T |
ー | ー |
| Range / RangeFrom / RangeWithSteps | ー | ー | ー |
| Clamp | ー | ー | ー |
| Sum | ー | ー | ー |
| SumBy | ー | ー | ー |
| Mean | ー | ー | ー |
| MeanBy | ー | ー | ー |
| RandomString | ー | ー | ー |
| Substring | string |
ー | ー |
| ChunkString | string |
ー | ー |
| RuneLength | string |
ー | ー |
| PascalCase | string |
ー | ー |
| CamelCase | string |
ー | ー |
| KebabCase | string |
ー | ー |
| SnakeCase | string |
ー | ー |
| Words | string |
ー | ー |
| Capitalize | string |
ー | ー |
| Ellipsis | string |
ー | ー |
| T2 -> T9 | ー | ー | ー |
| Unpack2 -> Unpack9 | ー | ー | ー |
| Zip2 -> Zip9 | ー | ー | ー |
| ZipBy2 -> ZipBy9 | ー | ー | ー |
| Unzip2 -> Unzip9 | ー | ー | ー |
| UnzipBy2 -> UnzipBy9 | ー | ー | ー |
| ChannelDispatcher | ー | ー | ー |
| SliceToChannel | ー | ー | ー |
| Generator | ー | ー | ー |
| Buffer | ー | ー | ー |
| BufferWithTimeout | ー | ー | ー |
| FanIn | ー | ー | ー |
| FanOut | ー | ー | ー |
| Contains | []T |
ー | ー |
| ContainsBy | []T |
✅ | ✅ |
| Every | []T |
ー | ー |
| EveryBy | []T |
✅ | ー |
| Some | []T |
ー | ー |
| SomeBy | []T |
✅ | ー |
| None | []T |
ー | ー |
| NoneBy | []T |
🔨 | ー |
| Intersect | []T |
ー | ー |
| Difference | []T |
ー | ー |
| Union | []T |
ー | ー |
| Without | []T |
ー | ー |
| WithoutEmpty | []T |
ー | ー |
| IndexOf | []T |
🔨 | ー |
| LastIndexOf | []T |
🔨 | ー |
| Find | []T |
✅ | ✅ |
| FindIndexOf | []T |
🔨 | ー |
| FindLastIndexOf | []T |
🔨 | ー |
| FindOrElse | []T |
ー | ー |
| FindKey | map[any]T |
ー | ー |
| FindKeyBy | map[any]T |
🔨 | 🔨 |
| FindUniques | []T |
ー | ー |
| FindUniquesBy | []T |
ー | ー |
| FindDuplicates | []T |
ー | ー |
| FindDuplicatesBy | []T |
ー | ー |
| Min | []T |
ー | ー |
| MinBy | []T |
🔨 | ー |
| Earliest | time |
ー | ー |
| EarliestBy | []T |
ー | ー |
| Max | []T |
ー | ー |
| MaxBy | []T |
🔨 | ー |
| Latest | []time.Time |
ー | ー |
| LatestBy | []T |
ー | ー |
| First | []T |
🔨 | ー |
| FirstOrEmpty | []T |
🔨 | ー |
| FirstOr | []T |
🔨 | ー |
| Last | []T |
🔨 | ー |
| LastOrEmpty | []T |
🔨 | ー |
| LastOr | []T |
🔨 | ー |
| Nth | []T |
🔨 | ー |
| Sample | []T |
🔨 | ー |
| Samples | []T |
🔨 | ー |
| Ternary | <condition> |
ー | ー |
| TernaryF | <condition> |
ー | ー |
| If / ElseIf / Else | <condition> |
ー | ー |
| Switch / Case / Default | <condition> |
ー | ー |
| IsNil | ー | ー | ー |
| ToPtr | ー | ー | ー |
| Nil | ー | ー | ー |
| EmptyableToPtr | ー | ー | ー |
| FromPtr | ー | ー | ー |
| FromPtrOr | ー | ー | ー |
| ToSlicePtr | []T |
✅ | ー |
| FromSlicePtr | []T |
✅ | ー |
| FromSlicePtrOr | []T |
🔨 | ー |
| ToAnySlice | []T |
ー | ー |
| FromAnySlice | ー | ー | ー |
| Empty | ー | ー | ー |
| IsEmpty | ー | ー | ー |
| IsNotEmpty | ー | ー | ー |
| Coalesce | ー | ー | ー |
| CoalesceOrEmpty | ー | ー | ー |
| Partial | ー | ー | ー |
| Partial2 -> Partial5 | ー | ー | ー |
| Attempt | ー | ー | ー |
| AttemptWhile | ー | ー | ー |
| AttemptWithDelay | ー | ー | ー |
| AttemptWhileWithDelay | ー | ー | ー |
| Debounce | ー | ー | ー |
| DebounceBy | ー | ー | ー |
| Synchronize | ー | ー | ー |
| Async | ー | ー | ー |
| Transaction | ー | ー | ー |
| WaitFor | ー | ー | ー |
| WaitForWithContext | ー | ー | ー |
| Validate | ー | ー | ー |
| Must | ー | ー | ー |
| Try | ー | ー | ー |
| Try1 -> Try6 | ー | ー | ー |
| TryOr | ー | ー | ー |
| TryOr1 -> TryOr6 | ー | ー | ー |
| TryCatch | ー | ー | ー |
| TryWithErrorValue | ー | ー | ー |
| TryCatchWithErrorValue | ー | ー | ー |
| ErrorsAs | ー | ー | ー |
$ go generate ./example
$ go run ./exampleIf you like this package and would like to generate more, please refer to the following.
- go-gen-slice-accessors: Generate accessors for each field in the struct.
- Support all slice lo.
- Support map struct as well as slice.
