Go 語言移除切片中的空值


在本文中,我們將學習如何使用各種示例從切片中移除空值。切片就像陣列一樣,是一系列元素的序列。陣列是一系列固定的元素,而切片是動態陣列,這意味著它的值不是固定的,可以更改。切片比陣列更高效、更快,此外,它們是透過引用而不是透過值傳遞的。讓我們透過示例學習如何執行它。

方法 1:使用 For 迴圈

在這種方法中,我們將瞭解如何在外部函式中使用 for 迴圈從切片中移除空值。讓我們透過演算法和程式碼瞭解它是如何完成的。

語法

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

append 函式用於向陣列切片新增值。它接受多個引數。第一個引數是要向其新增值的陣列,後跟要新增的值。然後,該函式返回包含所有值的最終陣列切片。

演算法

  • 步驟 1 − 建立一個名為 main 的包,並在程式中宣告 fmt(格式化包),其中 main 生成可執行程式碼,fmt 幫助格式化輸入和輸出。

  • 步驟 2 − 建立一個 main 函式,並在該函式中建立一個切片,其中包含一些值,包括空值。

  • 步驟 3 − 建立一個名為 removenullvalue 的函式,其中包含一個切片作為引數。

  • 步驟 4 − 建立一個名為 result 的空切片,此結果將用於向其中追加非空元素。

  • 步驟 5 − 執行一個迴圈,直到切片的長度,並在每次迭代中檢查切片的元素是否不等於空,將這些元素追加到 result 中,並移動到下一個迭代。

  • 步驟 6 − 迴圈終止後,將輸出切片返回到函式。

  • 步驟 7 − 輸出切片將使用 fmt.Println() 函式列印到控制檯,其中 ln 表示換行。

示例

Go 語言程式,使用 for 迴圈從示例中的切片中移除空值。

package main
import "fmt"
func removenullvalue(slice []interface{}) []interface{} {
	var output []interface{}
	for _, element := range slice {
		if element != nil {   //if condition satisfies add the elements in new slice
			output = append(output, element)
		}
	}
	return output  //slice with no nil-values
}
func main() {
	slice := []interface{}{10, 20, nil, 30, nil, 40}  //create slice
	fmt.Println("The original slice is:", slice)
	slice = removenullvalue(slice)
	fmt.Println("The slice after removal of null value is:")
	fmt.Println(slice) // Output: [1 2 3 4]
}

輸出

The original slice is: [10 20  30  40]
The slice after removal of null value is:
[10 20 30 40]

方法 2:使用過濾器

在此示例中,我們將瞭解如何在外部函式中使用 for 迴圈從切片中移除空值。讓我們透過演算法和程式碼瞭解它是如何完成的。

語法

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

append 函式用於向陣列切片新增值。它接受多個引數。第一個引數是要向其新增值的陣列,後跟要新增的值。然後,該函式返回包含所有值的最終陣列切片。

演算法

  • 步驟 1 − 建立一個名為 main 的包,並在程式中宣告 fmt(格式化包),其中 main 生成可執行程式碼,fmt 幫助格式化輸入和輸出。

  • 步驟 2 − 建立 main 函式,並在該函式中建立一個包含非空值和空值的切片。

  • 步驟 3 − 呼叫一個名為 removenullelement 的函式,其中包含一個切片作為引數。

  • 步驟 4 − 在 removenullelement 函式中,呼叫 filter 函式,並將切片和過濾器作為輸入。

  • 步驟 5 − 在 filter 函式內部,建立一個名為 output 的空切片,該切片將用於追加切片的元素。

  • 步驟 6 − 執行一個迴圈,直到切片的長度,並且 filter 函式將返回一個滿足過濾條件的新切片。

  • 步驟 7 − removenullelement 函式將獲得返回的切片,該函式將使用 filter 函式從切片中移除所有空值,並將其返回到 main 函式。

  • 步驟 8 − 新切片將使用 fmt.Println() 函式列印到控制檯,其中 ln 表示換行。

示例

Go 語言程式,使用過濾器從示例中的切片中移除空值。

package main
import "fmt"
func removenullelement(slice []interface{}) []interface{} {
	return filter(slice, func(i interface{}) bool {
		return i != nil
	})
}
func filter(slice []interface{}, f func(interface{}) bool) []interface{} {
	var output []interface{}
	for _, element := range slice {
		if f(element) {
			output = append(output, element)  //the values that satisfy filter will be appended in the output
		}
	}
	return output
}
func main() {
	slice := []interface{}{1, 2, nil, 3, nil, 4}  //create slice
	fmt.Println("The original slice is:", slice)
	slice = removenullelement(slice)
	fmt.Println("The slice after removing null element is:")
	fmt.Println(slice) // Output: [1 2 3 4]  
}

輸出

The original slice is: [1 2  3  4]
The slice after removing null element is:
[1 2 3 4]

結論

我們使用兩個示例執行了從切片中移除空元素的程式。在第一種方法中,我們使用 for 迴圈移除空元素,在第二種方法中,我們使用過濾器方法移除空值。這兩個示例給出類似的結果。

更新於: 2023年1月23日

2K+ 瀏覽量

啟動你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.