Is it though?

You would have the same problem if you thought the size of mySlice was always small, and it wasn't - range could be quite expensive (or not) - and if that was not valid workable you should be using something other than a slice.

You need understanding of the types and cost of operations in any language, and the naming (or something else) should make the types easily recognizable (and understandable).


-----Original Message-----
From: Tyler Compton
Sent: Nov 5, 2019 3:57 PM
To: toddsu...@icloud.com
Cc: golang-nuts
Subject: Re: [go-nuts] Why doesn't Go include a contains method for things like slices?

Ian's answer addresses your question about the absence of a slice.Contains method, but there have been discussions in the past about adding such a feature to the language itself as well. You brought up the idea of a builtin "contains" function, and I've seen others suggest adding something like Python's "in" operator as well. To most of these proposals, the answer has been "no" because of concerns about hiding a potentially very expensive operation behind a builtin or operator. There's no doubt that:

return myObj in mySlice

... is easier to miss than:

for _, obj := range mySlice {
    if obj == myObj {
        return true
    }
}
return false

On Tue, Nov 5, 2019 at 12:30 PM toddsurfs via golang-nuts <golang-nuts@googlegroups.com> wrote:
Sorry if this question reveals my newness to Go. I tried searching the mailing list but I couldn't find anything in a quick search. It seems like many languages include the ability to check if an array, or slice contain a particular value. I understand that you could do this easily with a for loop or alternatively use a map instead. However, is there a reason why Go doesn't just have slice.Contains(interface{}) or maybe a builtin contains(mySlice, interface{}) ?

I assume one reason might be because slices could potentially be really large? Or maybe the community feels that a simple for loop is so easy that it is not necessary? I would love to understand this more.

Thank You!
-Todd

--
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/7d4c52e3-8c0f-4ba6-ae24-67ddb4a07ede%40googlegroups.com.

--
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAA%3DXfu33vuhzU1krGJKUZNaj6fNOhYQQY4%2BBfNtsdq0o7Q116w%40mail.gmail.com.




-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/1726366586.12454.1572993861364%40wamui-abby.atl.sa.earthlink.net.

Reply via email to