Golang程序 在切片中搜索一个元素

Golang程序 在切片中搜索一个元素

在本教程中,我们将通过不同的例子来掌握如何在slice中搜索一个元素。切片是一个元素的序列,就像数组一样。数组是一个固定的元素序列,而slice是一个动态数组,这意味着它的值是不固定的,可以改变。分片比数组更有效率和速度,而且它们是通过引用而不是通过值来传递的。

语法

func append(slice, element_1, element_2…, element_N) []T

append函数用于向一个数组片断添加值。它需要一些参数。第一个参数是我们希望添加的数组,后面是要添加的值。然后该函数返回包含所有值的数组的最终片断。

方法1:使用一个外部用户定义的函数

在这个方法中,我们将使用一个外部函数来搜索分片中的元素。切片和要搜索的元素将作为参数传入函数。输出将使用fmt.Println()函数打印在控制台。让我们通过代码看看这一切是如何完成的。

算法

  • 第1步 – 创建一个包main,并在程序中声明fmt(format package)包,其中main产生可执行代码,fmt帮助格式化输入和输出。

  • 第2步 – 创建一个名为search_ele的函数,以slice和要搜索的元素为参数,该函数由main调用。

  • 第3步 – 运行一个循环直到slice的长度,并检查要搜索的元素是否等于slice的任何元素

  • 第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函数来搜索片断中的元素。一个标志将被创建,它的值将帮助我们打印该元素是否存在于片断中。输出将使用fmt.Println()函数打印在控制台。让我们通过代码看看这一切是如何完成的。

算法

  • 第1步 – 创建一个包main,并在程序中声明fmt(format package)包,其中main产生可执行代码,fmt帮助格式化输入和输出。

  • 第2步 – 创建一个main函数,并在该函数中使用append函数创建一个片断,以及一个初始值为false的bool类型的变量flag。

  • 第3步 – 创建一个变量item,并将要搜索的值赋给它。

  • 第4步– 运行一个循环直到片断的长度,并检查要搜索的元素是否等于片断的任何元素。

  • 第5步 – 如果为真,则将标志设为真,并中断循环,但如果不为真,则将循环运行到结束,在循环结束后检查一个条件。

  • 第6步 – 如果标志为真,则打印该元素存在于片断中的语句,否则打印该元素不存在于片断中。

  • 第7步 – 使用fmt.Println()函数执行打印语句,ln代表新行。

例子

Golang程序使用main函数在片断中搜索一个元素

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

结论

我们用两个例子执行了搜索片断元素的程序。在第一个例子中,我们使用了一个自定义函数来搜索元素,在第二个例子中,我们使用了主函数来搜索值。这两个例子都给出了类似的输出。因此,该程序成功执行。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程