Go语言程序:在切片中搜索元素


在本教程中,我们将学习如何使用不同的示例在切片中搜索元素。切片就像数组一样,是一系列元素的序列。数组是一系列固定元素的序列,而切片是动态数组,这意味着它的值不是固定的,可以更改。切片比数组更高效、更快,并且它们是通过引用而不是通过值传递的。

语法

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 − 调用 main 函数。

  • 步骤 6 − 在 main 函数中,检查该值是否等于 -1,如果等于 -1,则打印该元素不存在于切片中,否则打印它存在于切片中。

  • 步骤 7 − 打印语句使用 fmt.Println() 函数执行,其中 ln 表示换行符。

示例

使用外部函数在切片中搜索元素的 Go 语言程序

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(格式化包),其中 main 生成可执行代码,fmt 帮助格式化输入和输出。

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

  • 步骤 3 − 创建一个变量 item 并为其分配要搜索的值。

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

  • 步骤 5 − 如果为真,则将标志设置为 true 并中断循环,但如果为假,则运行循环直到结束,并在循环终止后检查条件。

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

  • 步骤 7 − 打印语句使用 fmt.Println() 函数执行,其中 ln 表示换行符。

示例

使用 main 函数在切片中搜索元素的 Go 语言程序

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

结论

我们使用两个示例执行了搜索切片元素的程序。在第一个示例中,我们使用自定义函数搜索元素,在第二个示例中,我们使用 main 函数搜索值。这两个示例都提供了类似的输出。因此,程序成功执行。

更新于: 2023年1月17日

3K+ 浏览量

开启您的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.