Golang 用于在切片中搜索元素
在本教程中,我们将掌握如何使用不同的示例在切片中搜索元素。切片是类似于数组的元素序列。数组是一系列固定元素,而切片是动态数组,这意味着它的值是不固定的,并且可以更改。切片比数组更高效和更快,而且它们是通过引用传递而不是值传递。
语法
func append(slice, element_1, element_2…, element_N) []T
append函数用于向数组切片添加值。它接受多个参数。第一个参数是要向其添加值的数组,其后是要添加的值。然后函数返回包含所有值的最终数组切片。
方法1:使用外部用户定义的函数
在这种方法中,我们将使用外部函数在切片中搜索元素。切片和要搜索的元素将作为参数传递给函数。输出将使用fmt.Println()函数打印在控制台上。让我们通过代码来看看如何完成所有这些操作。
步骤
- 第1步 − 创建一个main包并在程序中声明fmt(格式化包)包,其中main产生可执行代码,fmt帮助格式化输入和输出。
-
第2步 − 创建一个名为search_ele的函数,其参数为切片和要搜索的元素,并从main函数中调用该函数。
-
第3步 − 运行循环直到切片的长度,并检查要搜索的元素是否等于切片的任何元素。
-
第4步 − 如果为真,则返回索引;如果为假,则返回-1给自定义函数。
-
第5步 − 调用主函数。
-
第6步 − 在主函数中,检查值是否等于-1,如果是,则打印元素不存在于切片中,否则打印元素存在于切片中。
-
第7步 − 使用fmt.Println()函数执行打印语句,ln表示换行。
示例
使用外部函数在切片中搜索元素的Golang程序
package main
import "fmt"
func main() {
// Declare a slice of integers
var slice []int
slice = append(slice, 10) // create slice using append function
slice = append(slice, 20)
slice = append(slice, 30)
slice = append(slice, 40)
slice = append(slice, 50)
fmt.Println("The slice given here is:", slice)
// Call the search function and store the value in a variable named val
val := search_ele(slice, 40)
fmt.Println("The value to be searched from the slice is:", 40)
if val != -1 {
fmt.Println("The element is found in slice at index:", val)
} else {
fmt.Println("The element was not found in the slice")
}
}
func search_ele(slice []int, key int) int {
for i, element := range slice {
if element == key { // check the condition if its true return index
return i
}
}
return -1
}
输出
The slice given here is: [10 20 30 40 50]
The value to be searched from the slice is: 40
The element is found in slice at index: 3
方法2:使用main函数
在这种方法中,我们将使用main函数在切片中搜索元素。将创建一个标志(flag),其值将帮助我们打印出元素是否存在于切片中。输出将使用fmt.Println()函数打印在控制台上。让我们通过代码来看看这一切是如何完成的。
步骤
- 步骤1 - 创建一个main包并在程序中声明fmt(格式化包)包,其中main产生可执行代码,fmt帮助格式化输入和输出。
-
步骤2 - 创建一个main函数,并在函数中使用append函数创建一个切片,并创建一个类型为bool且初始值为false的变量flag。
-
步骤3 - 创建一个变量item,并将其赋予要搜索的值。
-
步骤4 - 运行循环直到切片的长度,并检查要搜索的元素是否等于切片的任何元素。
-
步骤5 - 如果条件为true,则将flag设置为true并跳出循环,否则继续循环直到结束,并在循环结束后检查一个条件。
-
步骤6 - 如果flag为true,则打印元素存在于切片中的语句,否则打印元素不存在于切片中的语句。
-
步骤7 - 使用fmt.Println()函数执行打印语句,其中ln表示换行。
示例
使用main函数在切片中搜索元素的Golang程序
package main
import "fmt"
func main() {
var slice []int
slice = append(slice, 10) // create slice using append function
slice = append(slice, 20)
slice = append(slice, 30)
slice = append(slice, 40)
slice = append(slice, 50)
var flag bool = false // assign initial value as false
fmt.Println("The slice given here is:", slice)
var item int = 8
fmt.Println("The value to be searched from the slice is:", item)
for element := range slice {
if element == item {
flag = true // break the loop if flag is true
break
}
}
if flag {
fmt.Println("The element is present in the slice")
} else {
fmt.Println("The element is not present in the slice")
}
}
输出
The slice given here is: [10 20 30 40 50]
The value to be searched from the slice is: 8
The element is not present in the slice
结论
我们执行了一个程序来搜索切片中的元素,使用了两个示例。在第一个示例中,我们使用了一个自定义函数来搜索元素,在第二个示例中,我们使用了主函数来搜索值。这两个示例都给出了类似的输出。因此,该程序成功执行。
极客笔记