Add "in" keyword for if statement

328 views
Skip to first unread message

jon...@gmail.com

unread,
Apr 28, 2014, 2:00:43 PM4/28/14
to golan...@googlegroups.com
Hey all,

Yesterday I was writing a code where I needed to check if a value was in an array. The only way I found to do it was writing my own IsInArray function, there were no other way to do it, at least I didn't find it.

So I thought we could add a new "in" keyword like the one in existent in Python.

The syntax would be like this:

el := "a"
vowels := []string{"a", "e", "i", "o", "u", "y"}

if el in vowels {
   // do something here
}

What do you think about that?

Jesse McNelis

unread,
Apr 29, 2014, 12:08:29 AM4/29/14
to jon...@gmail.com, golang-nuts
On Tue, Apr 29, 2014 at 4:00 AM, <jon...@gmail.com> wrote:
> el := "a"
> vowels := []string{"a", "e", "i", "o", "u", "y"}
>
> if el in vowels {
> // do something here
> }
>
> What do you think about that?

Go tries to avoid making O(n) operations look like they aren't O(n).
The loop is three lines and it's simple enough to put it in a function
if you use it more than once.

This also ties more things to the language's definition of equality,
and equality is generally vague and domain specific.

Ingo Oeser

unread,
Apr 29, 2014, 1:54:08 AM4/29/14
to golan...@googlegroups.com, jon...@gmail.com
There is a stdlib version for your use case: strings.Contains()

vowels := "aeiouy"
el := "a"

if strings.Contains(vowels, el) {
        fmt.Println(el, "is a vowel")

Jonas Trevisan

unread,
Apr 29, 2014, 8:04:16 AM4/29/14
to Ingo Oeser, golan...@googlegroups.com
Yes, but if I need to search in an array of strings "[]string" how would I do that? Do I need to write my own function to do it?

Andrew Lytvynov

unread,
Apr 29, 2014, 8:14:15 AM4/29/14
to golan...@googlegroups.com, Ingo Oeser, jon...@gmail.com
Perhaps you actually want a map[string]struct{} which has O(1) lookup?

Jan Mercl

unread,
Apr 29, 2014, 8:14:41 AM4/29/14
to Jonas Trevisan, Ingo Oeser, golang-nuts
On Tue, Apr 29, 2014 at 2:04 PM, Jonas Trevisan <jon...@gmail.com> wrote:
> Yes, but if I need to search in an array of strings "[]string" how would I
> do that? Do I need to write my own function to do it?

Most often not a function but you code search in a for statement. Or
you can, if not sorted already, do sort.StringSlice(myStrings) +
sort.SearchStrings(myStrings, myString). For short []string the 'for'
approach is okay.

for i, v := range vowels {
if v == el {
// do something here
}
}

-j

Jonas Trevisan

unread,
Apr 29, 2014, 9:05:40 AM4/29/14
to Andrew Lytvynov, golan...@googlegroups.com, Ingo Oeser
Well, I could do something like this:
var m = map[string]struct{}{
    "a": {},
    "e": {},
}

func main() {
    el := "f"
    if _, ok := m[el]; ok {
    fmt.Println(el)
    }
}

Although that map with empty values sound a bit strange to me

Jonas Trevisan

unread,
Apr 29, 2014, 9:11:02 AM4/29/14
to Jan Mercl, Ingo Oeser, golang-nuts
I think I will loose performance in the approach on sorting the slice and then searching for values. I'm currently using the 'for' approach, I think there should be a better way to do it though.

Andrew Lytvynov

unread,
Apr 29, 2014, 9:19:43 AM4/29/14
to Jonas Trevisan, golan...@googlegroups.com, Ingo Oeser
This is the idiomatic way of implementing sets in Go.

James Bardin

unread,
Apr 29, 2014, 9:33:00 AM4/29/14
to golan...@googlegroups.com, Jan Mercl, Ingo Oeser, jon...@gmail.com


On Tuesday, April 29, 2014 9:11:02 AM UTC-4, Jonas Trevisan wrote:
I think I will loose performance in the approach on sorting the slice and then searching for values. I'm currently using the 'for' approach, I think there should be a better way to do it though.



The keyword "in" you want is just hiding a for loop. How else would you find an item in an unsorted slice/array/list without a linear search? If you're searching more than once, sorting and using a binary search would be better, or possibly use a map which was also suggested.

As Jesse said earlier, Go tries not to hide algorithmic complexity, and making an O(n) look like O(1) would do just that. 

Jonas Trevisan

unread,
Apr 29, 2014, 9:44:29 AM4/29/14
to James Bardin, golang-nuts, Jan Mercl, Ingo Oeser
I did some research here and I found that adding the "in" keyword would just add more complexity to the language.
I could just use the sort approach in big arrays and the "for" or a "map" in small ones.
Please correct me if I'm wrong and thank you for your thoughts.

Kevin Gillette

unread,
Apr 29, 2014, 10:14:23 AM4/29/14
to golan...@googlegroups.com, jon...@gmail.com
You can be sure that it was not overlooked during the design of the language. The problem with 'in' is that it's easy to abuse: linear slice searching has very poor performance. Any situation requiring frequent slice searches (or searches over large slices) will invariably be better off using either maps, or binary searching over sorted slices.

If you have trivial cases that can justify the use of linear search, then a SliceContains function really is trivial to write.

Matt Harden

unread,
Apr 29, 2014, 9:58:51 PM4/29/14
to Kevin Gillette, golang-nuts, jon...@gmail.com
I'm not sure how it performs compared to the other approaches, but if the list of strings is fixed and relatively small, a switch statement would do the job also:

switch el {
case "a", "e", "i", "o", "u", "y":
// do something here
}



--
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.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages