Is there anything similar to a slice.contains(object)
method in Go without having to do a search through each element in a slice?
相关问题
- Golang mongodb aggregation
- How to flatten out a nested json structure in go
- how to install private repo using glide golang
- How to convert a string to a byte array which is c
- IntelliJ 2017.1.2 GOLANG debug does not work on br
相关文章
- Can I run a single test in a suite?
- How to check if a request was cancelled
- Is it possible to implement an interface with unex
- How to access value of first index of array in Go
- Embedded Interface
- Why does slice [:-0] return empty list in Python
- Python negative zero slicing
- How to represent an array with mixed types
If it is not feasable to use a map for finding items based on a key, you can consider the goderive tool. Goderive generates a type specific implementation of a contains method, making your code both readable and efficient.
Example;
To generate the deriveContainsFoo method:
go get -u github.com/awalterschulze/goderive
goderive ./...
in your workspace folderThis method will be generated for deriveContains:
Goderive has support for quite some other useful helper methods to apply a functional programming style in go.
No, such method does not exist, but is trivial to write:
You can use a map if that lookup is an important part of your code, but maps have cost too.