samber / lo

💥 A Lodash-style Go library based on Go 1.18+ Generics (map, filter, contains, find...)
https://pkg.go.dev/github.com/samber/lo
MIT License
17.88k stars 821 forks source link

How to use the `lo` more easily #254

Closed SimFG closed 2 years ago

SimFG commented 2 years ago

For example, if I want to iterate over the names collection, I will type lo.ForEach(names, func...). I need to complete the generics type and look that the function need to fill those params in the GoLand.

I will spend more time filling relevant content than using a for statement. I don't know if there is a suitable plugin here to solve this problem.

samber commented 2 years ago

I'm not sure to understand your issue.

If a "for" loop looks better, why not using it?

2 cases not supported by "for" loop:

SimFG commented 2 years ago

When I use some lo method, I don't know how many parameters this function takes and what are the parameter types. If I don't look at the source implementation of this function, I will not be able to use this function. But if the parameter of a function is a function type, I only need to enter func, goland will give a prompt, and then press Enter, I can complete the structure of the function, including which parameters are required, what types of parameters, and return results. 20221024-214115

SimFG commented 2 years ago

About the second case, "copy" problem: in a "for" loop, next value has the same memory address, so you cannot use it as a pointer. ForEach solves that issue.

I can't understand it. Can you can write an example to explain it. Thank you.

samber commented 2 years ago

I can't understand it. Can you can write an example to explain it. Thank you.

https://github.com/golang/go/wiki/CommonMistakes#using-reference-to-loop-iterator-variable

samber commented 2 years ago

About autocompletion in your code editor:

SimFG commented 2 years ago

About the autocompletion settings, can you share me about this part of settings or plugins in the VSCode.

samber commented 2 years ago

I use the following extension: https://marketplace.visualstudio.com/items?itemName=golang.Go

SimFG commented 2 years ago

Thanks for your detailed answers.