Created
April 5, 2018 11:55
-
-
Save bojand/c6c730efee557ad50bf616a6bcbd757f to your computer and use it in GitHub Desktop.
nil and len() in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
type Foo struct{} | |
func main() { | |
var nilPtr *Foo | |
var sliceVar []string | |
emptySlice := []string{} | |
madeSlice := make([]string, 0) | |
var slicePtrs []*Foo | |
emptyMap := map[string]interface{}{} | |
madeMap := make(map[string]interface{}) | |
fmt.Printf("nilPtr == nil ? %+v\n", nilPtr == nil) | |
fmt.Printf("sliceVar == nil ? %+v\n", sliceVar == nil) | |
fmt.Printf("emptySlice == nil ? %+v\n", emptySlice == nil) | |
fmt.Printf("madeSlice == nil ? %+v\n", madeSlice == nil) | |
fmt.Printf("slicePtrs == nil ? %+v\n", slicePtrs == nil) | |
fmt.Printf("emptyMap == nil ? %+v\n", emptyMap == nil) | |
fmt.Printf("madeMap == nil ? %+v\n", madeMap == nil) | |
fmt.Printf("len(sliceVar): %+v\n", len(sliceVar)) | |
fmt.Printf("len(emptySlice): %+v\n", len(emptySlice)) | |
fmt.Printf("len(madeSlice): %+v\n", len(madeSlice)) | |
fmt.Printf("len(emptyMap): %+v\n", len(emptyMap)) | |
fmt.Printf("len(madeMap): %+v\n", len(madeMap)) | |
fmt.Printf("len(slicePtrs): %+v\n", len(slicePtrs)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment