Go语言程序:从切片中获取第一个和最后一个元素


在本教程中,我们将讨论如何从切片中获取第一个和最后一个元素。切片是一个动态序列,用于存储相同类型的元素。它类似于数组,但可以调整大小,而数组的大小是固定的。让我们通过一些例子来理解这个概念。

方法一:使用整数值

在这种方法中,我们将使用整数值来从切片中获取第一个和最后一个元素。输出将使用 fmt.Println() 函数打印到屏幕上。让我们通过示例来了解如何实现。

算法

  • 步骤 1 − 创建一个名为 main 的包,并在程序中导入 fmt 包。

  • 步骤 2 − 创建一个名为 main 的函数,并在函数中创建一个名为 slice_demo 的切片,并用需要计算第一个和最后一个元素的值填充它。

  • 步骤 3 − 使用切片名称 slice_demo[0] 从切片中获取第一个元素,其中 0 指的是第一个元素。

  • 步骤 4 − 使用 Go 语言中的 fmt.Println() 函数打印第一个元素,其中 ln 表示换行。

  • 步骤 5 − 使用 slice_demo[len(slice_demo)-1] 获取最后一个元素,并像步骤 4 中那样打印它。

示例

使用整数值从切片中获取第一个和最后一个元素的 Go 语言程序

package main
import "fmt"

// create a function main 
func main() {
   // create a slice and fill it with required elements
   slice_demo := []int{1,2,3,4,5}

   fmt.Println("The demo slice is:")

   fmt.Println(slice_demo)

   // obtain the first element
   first_ele := slice_demo[0]

   fmt.Println("The first element of demo slice is:")

   // print the first element
   fmt.Println(first_ele)

   // obtain the last element using the logic
   last_ele := slice_demo[len(slice_demo)-1]

   fmt.Println("The last element of demo slice is:")
                
   // print the last element
   fmt.Println(last_ele)

}

输出

The demo slice is:
[1 2 3 4 5]
The first element of demo slice is:
1
The last element of demo slice is:
5

方法二:使用浮点值

在这种方法中,我们将使用浮点值来从切片中获取第一个和最后一个元素。输出将使用 fmt.Println() 函数打印到屏幕上。让我们深入研究示例,看看它是如何工作的。

算法

  • 步骤 1 − 创建一个名为 main 的包,并在程序中导入 fmt 包。

  • 步骤 2 − 创建一个名为 main 的函数,并在函数中创建一个名为 slice_demo 的切片,并用需要计算第一个和最后一个元素的整数值填充它。

  • 步骤 3 − 使用切片名称 slice_demo[0] 从切片中获取第一个元素,其中 0 指的是第一个元素。

  • 步骤 4 − 使用 Go 语言中的 fmt.Println() 函数打印第一个元素,其中 ln 表示换行。

  • 步骤 5 − 使用 slice_demo[len(slice_demo)-1] 获取最后一个元素,并像步骤 4 中那样打印它。

示例

使用浮点值从切片中获取第一个和最后一个元素的 Go 语言程序

package main
import "fmt"

// create a function main to execute the program
func main() {
            
   // create a slice and fill it with float elements
   slice_demo := []float32{1.8, 3.4, 2.0, 3.9, 4.6}
   fmt.Println("The demo slice is:")
   fmt.Println(slice_demo)
                 
   // fetch the first element using the following logic
   first_ele := slice_demo[0]
   fmt.Println("The first element of demo slice is:")

   //print the first element 
   fmt.Println(first_ele)

   // fetch the last element using the logic here
   last_ele := slice_demo[len(slice_demo)-1]
   fmt.Println("The last element of demo slice is:")

   // print the last element
   fmt.Println(last_ele)

}

输出

The demo slice is:
[1.8 3.4 2 3.9 4.6]
The first element of demo slice is:
1.8
The last element of demo slice is:
4.6

方法三:在切片中使用 append 方法

在这种方法中,我们将使用 append 方法创建一个切片,然后在这个切片中获取第一个和最后一个元素。输出将使用 fmt.Println() 函数打印到屏幕上。让我们深入研究示例,看看它是如何工作的。

语法

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

append 函数用于向数组切片添加值。它接受多个参数。第一个参数是要向其中添加值的数组,后跟要添加的值。然后,该函数返回包含所有值的最终数组切片。

算法

  • 步骤 1 − 创建一个名为 main 的包,并在程序中导入 fmt 包。

  • 步骤 2 − 创建一个名为 main 的函数,并在函数中使用 append 函数创建一个切片,并将切片打印到控制台。

  • 步骤 3 − 使用切片名称 slice[0] 从切片中获取第一个元素,其中 0 指的是第一个元素。

  • 步骤 4 − 使用 Go 语言中的 fmt.Println() 函数打印第一个元素,其中 ln 表示换行。

  • 步骤 5 − 使用 slice[len(slice)-1] 获取最后一个元素,并像步骤 4 中那样打印它。

示例

在切片中使用 append 方法从切片中获取第一个和最后一个元素的 Go 语言程序

package main
import "fmt"

func main() {

   var slice []int  // create an empty slice
                
   //append the elements in slice using append function

   slice = append(slice, 1)
   slice = append(slice, 2)
   slice = append(slice, 3)
   slice = append(slice, 4)

   fmt.Println("The demo slice is:")
   fmt.Println(slice)

   // obtain the first element
   first_ele := slice[0]   

   fmt.Println("The first element of demo slice is:")
   fmt.Println(first_ele)

   // obtain the last element
   last_ele := slice[len(slice)-1]

   fmt.Println("The last element of demo slice is:")
   fmt.Println(last_ele)

}

输出

The demo slice is:
[1 2 3 4]
The first element of demo slice is:
1
The last element of demo slice is:
4

结论

我们使用三种方法执行了查找切片第一个和最后一个元素的程序。在第一个示例中,我们在 main 函数中使用了整数值来执行程序。在第二个示例中,我们使用了浮点值来实现相同的目的,在第三个示例中,我们使用了 append 函数。所有示例都返回了相似的输出。因此,程序成功执行。

更新于:2023年1月17日

1K+ 阅读量

开启您的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.