我有一個函數(shù),它接受指向任何切片的指針,并且所述參數(shù)的類型為interface{}。我需要確保指針不指向切片nil。package mainimport ( "fmt")type Dog struct { Name string Age int}func main() { var slicePointer interface{} // We only want slices that were initialized with `make()`. dogs1 := make([]Dog, 0) fmt.Println("dogs1 == nil:", dogs1 == nil) slicePointer = &dogs1 fmt.Println("slicePointer == nil:", slicePointer == nil) fmt.Println("-----") // This needs to be caught, as the slice is `nil`. var dogs2 []Dog fmt.Println("dogs2 == nil:", dogs2 == nil) slicePointer = &dogs2 fmt.Println("slicePointer == nil:", slicePointer == nil)}輸出:dogs1 == nil: falseslicePointer == nil: false-----dogs2 == nil: trueslicePointer == nil: false 有沒有辦法在 Golang 中進行這樣的檢查?
檢查指針是否指向 Nil Slice
12345678_0001
2023-08-07 18:56:15