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 函式。所有示例都返回了相似的輸出。因此,程式成功執行。