> Why can I not append something to a Slice in place, why does it have to
> return a new Slice?
https://blog.golang.org/slices
> 2) find(sliceB, item_i_look_for) int, -1 for not found
>
> Why can I not find easily search for an item inside a Slice. Every other
> language I know has this. And I need this in a generic form.
As you have probably heard, Go does not have generics. Sorry. The
loop that you need to write is fairly short.
Because it's easy to write a short correct find loop for the type you
are using, but a good implementation of append is a much larger
function. See also http://www.airs.com/blog/archives/559 .
In my everyday programming life a check if an item is inside an Array is very common. Often thisis even built-in syntax (like in Python "if a in array...").I will have a hard time selling this missing feature to my team.Martin
-j
The Go philosophy is explicitly not to give you everything you want. It is to give you everything you need to build everything you want, like Lego.
Every language is different. Any developer worth their salt won't dismiss a tool out-of-hand for such a trivial reason.
Also, consider the fact that in Python, the same loop is happening. Go just doesn't hide that from the developer, making it easier for us to reason about things like performance. You can write your own "find" function in seconds if you want one.
--☕😎
-j
Martin--☕😎--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
[...] So why not come up with
-j
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
Volker thanks for the reply. Coming from Python I can do a simple test likeif item in my_array:# do somethingelse:# do something elseI can understand that it cannot be that easy in Go (and then, why not, "equals" is already defined), but image youhave to use a for-loop every time or a specialized function every time. That is awkward and unnecessary.
I agree, that the more complex idea to have a "findFirst" with a dedicated matching functionis more complicated, but hey, sort.Slice is the same thing, isn't it? It takes a comparison func to return -1, 0, or 1.