A very common programming problem that arises in most languages is a simple function that returns true if an input item is present in an array (or dictionary or slice or a variety of other terms) of items and false if it is not.
在大多数语言中出现的一个非常常见的编程问题是一个简单的函数,如果输入项存在于项的数组(或字典或切片或各种其他术语)中,则返回true否则返回false 。
Basically, let’s say we had a grouping of items like this:
基本上,我们说我们有如下一组项目:
[cat, dog, bird, turtle, fish, panda]In my program, I simply want to check if the string gopher exists. In this case, the function I use will return false.
在我的程序中,我只想检查字符串gopher存在。 在这种情况下,我使用的函数将返回false 。
The thing about Go is that this isn’t a standard, out-of-the-box function, so we’ll have to write it ourselves.
关于Go的事情是,这不是一个标准的即用型功能,因此我们必须自己编写。
You can pretty much stop reading here if that’s good enough, because if you’re somewhat familiar with Go, you can copy and paste that and get to using it right away.
如果这足够好,您可以在这里停止阅读,因为如果您对Go有所了解,则可以复制并粘贴并立即开始使用它。
In the code, we would call this function with something like this:
在代码中,我们将使用以下代码调用此函数:
found := Find(animalSlice, "gopher")func is just the syntax Go uses to declare a function.
func只是Go用来声明函数的语法。
Find(slice []string, sliceItem string) is the name of the function and the parameters that will be passed into the function and used within the function. Find can honestly be named whatever you want here, but it’s named this as a very basic description of what the function does. Lastly, in Go, the variable name of parameters come first while the type comes second.
Find(slice []string, sliceItem string)是函数的名称,以及将传递到函数中并在函数中使用的参数。 老实说, Find可以命名为您想要的任何名称,但这是对函数功能的非常基本的描述。 最后,在Go中,参数的变量名排在第一位,类型的排在第二位。
bool is the return type of the function.
bool是函数的返回类型。
for item := range slice is the way of iterating through the slice one item at a time. The := is kind of strange when you first see it, but it’s an assignment operator and is used in for statements in Go in general. For more on for statements, you can read here.
for item := range slice是一次迭代一个for item := range slice的方式。 当您第一次看到它时, :=有点奇怪,但是它是一个赋值运算符,通常用于Go中的for语句中。 有关for语句的更多信息,您可以在此处阅读。
The body of the for statement has a simple if check to see if the individual string in the slice matches with the input parameter string. In this case, as soon as we get one match, we have answered our question: the string does indeed exist in the slice, so the function can return true. If the loop finishes, then the default return value is false.
for语句的主体具有简单的if检查,以查看片中的各个字符串是否与输入参数字符串匹配。 在这种情况下,一旦我们找到一个匹配项,我们就回答了我们的问题:字符串的确确实存在于切片中,因此该函数可以返回true 。 如果循环结束,则默认返回值为false 。
For more information on the structure of functions, I recommend the Tour of Go’s introduction to functions.
有关功能结构的更多信息,我推荐Tour of Go的功能介绍。
Fortunately, once you understand the above section with strings, you can apply it to pretty much every type. Thus, seeing if one int exists in a slice of int items is as simple as this:
幸运的是,一旦您理解了带有字符串的上述部分,就可以将其应用于几乎每种类型。 因此,如果看到一个int在一片存在int项目很简单,只要这样的:
func Find(slice []int, sliceItem int) bool { for item := range slice { if item == sliceItem { return true } } return false}To do this for any type, then, simply replace the bolded type values with the appropriate item type.
为此,对于任何类型,只需将粗体类型的值替换为适当的项目类型即可。
The above solution has a time complexity of O(N) since the for loop iterates through the entire slice. If this is an operation you have to do over and over again, you probably would want to put the contents of the slice in a map, which has an upfront cost of O(N) but would have subsequent access costs of O(1) (and at worst O(log(N)) depending on the implementation of the map). (h/t Tim Jones)
上述解决方案的时间复杂度为O(N)因为for循环遍历整个分片。 如果您必须一遍又一遍地执行此操作,则可能需要将切片的内容放入地图中,该地图的前期成本为O(N)但随后的访问成本为O(1) (最糟糕的是O(log(N))具体取决于地图的实现)。 (小时蒂姆·琼斯)
翻译自: https://medium.com/swlh/how-to-write-a-go-function-that-returns-true-if-it-finds-one-item-string-integer-etc-a1917e7fab52