Is there a way to write generic code to find out whether a slice contains specific element in Go?

后端 未结 4 616
离开以前
离开以前 2021-01-20 13:38

I want to know is there a generic way to write code to judge whether a slice contains an element, I find it will frequently useful since there is a lot of logic to fist judg

4条回答
  •  醉话见心
    2021-01-20 14:14

    If you want a rather different solution, you might try the code-generator approach offered by tools such as Gen. Gen writes source code for each concrete class you want to hold in a slice, so it supports type-safe slices that let you search for the first match of an element.

    (Gen also offers a few other kinds of collection and allows you to write your own.)

提交回复
热议问题