C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
And: A slice (or interface) is at first uninitialized. It is nil. We can test for uninitialized things with nil.
SliceInterfaceIfGolang program that uses uninitialized slice
package main
import "fmt"
func main() {
// This is an uninitialized slice.
var temp []int
// The uninitialized slice is nil.
if temp == nil {
fmt.Println("Uninitialized slice is nil")
}
}
Output
Uninitialized slice is nil
Important: We cannot use nil in a slice or array of strings. The nil constant has no type—so it cannot substitute for a string.
Golang program that causes nil, string error
package main
func main() {
temp := []string{}
temp = append(temp, nil)
}
Output
# command-line-arguments
C:\programs\file.go:8: cannot use nil as type string in append
Quote: The value of an uninitialized slice is nil (Go Programming Language).