Golang 从切片中获取第一个和最后一个元素

Golang 从切片中获取第一个和最后一个元素

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

方法1:使用整数值

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

步骤

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

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

  • 步骤3 - 使用切片名称slice_demo[0]获取切片的第一个元素,这里的0表示第一个元素。

  • 步骤4 - 使用fmt.Println()函数在Golang中打印第一个元素,其中ln表示换行。

  • 步骤5 - 使用slice_demo[len(slice_demo)-1]获取最后一个元素,并以与步骤4中相似的方式打印它。

示例

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

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

方法2:使用浮点数值

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

步骤

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

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

  • 步骤3 - 使用切片名称slice_demo[0]获取第一个元素,其中0表示第一个元素。

  • 步骤4 - 使用fmt.Println()函数在Golang中打印第一个元素,其中ln表示下一行。

  • 步骤5 - 使用slice_demo[len(slice_demo)-1]获取最后一个元素,并类似于步骤4打印它。

示例

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

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

方法3:使用切片中的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 - 使用fmt.Println()函数和ln创建的换行符打印出第一个元素。

  • 步骤5 - 使用slice[len(slice)-1]获取最后一个元素,并以与步骤4相似的方式打印出来。

示例

使用append方法获取切片中的第一个和最后一个元素的Golang程序。

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

结论

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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程