Equivalent for Python's list comprehension

后端 未结 2 1025
隐瞒了意图╮
隐瞒了意图╮ 2020-12-30 22:10

I am playing with Go but I am having a very hard time doing things that are very simple in other languages.

I\'d like to reproduce a similar syntax:

         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-30 22:55

    If what you are looking is indeed python list comprehension, there is no such syntactic equivalent in go AFAIK.

    The way to do it is to create a function that take a slice and a function (to test the condition) and return a new slice.

    EDIT: Well looks like there is already such a feature in Go. cf VonC

提交回复
热议问题