This Gist contains the code examples from the blog post Mastering Slices in Go: Deep Dive into Nil vs Empty Slices, Capacity, and Length.
Created
July 28, 2024 14:39
-
-
Save arya2004/123f11027746a5a3920d274deb5323b7 to your computer and use it in GitHub Desktop.
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" | |
| ) | |
| func main() { | |
| // Declaring a nil slice | |
| var s []int | |
| printSliceInfo("s", s) | |
| // Declaring an empty slice | |
| t := []int{} | |
| printSliceInfo("t", t) | |
| // Declaring a slice with length 5 | |
| u := make([]int, 5) | |
| printSliceInfo("u", u) | |
| // Declaring a slice with length 0 but capacity 5 | |
| v := make([]int, 0, 5) | |
| printSliceInfo("v", v) | |
| } | |
| func printSliceInfo(name string, s []int) { | |
| fmt.Printf("%s: len=%d cap=%d nil=%t %v\n", name, len(s), cap(s), s == nil, s) | |
| } |
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" | |
| ) | |
| func main() { | |
| // Create a slice with length 5 | |
| u := make([]int, 5) | |
| printSliceInfo("u", u) | |
| // Create a slice with length 0 but capacity 5 | |
| v := make([]int, 0, 5) | |
| printSliceInfo("v", v) | |
| // Appending to slices | |
| v = append(v, 10) | |
| printSliceInfo("v after append", v) | |
| // Appending beyond capacity | |
| v = append(v, 20, 30, 40, 50, 60) | |
| printSliceInfo("v after more appends", v) | |
| } | |
| func printSliceInfo(name string, s []int) { | |
| fmt.Printf("%s: len=%d cap=%d %v\n", name, len(s), cap(s), s) | |
| } |
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 ( | |
| "encoding/json" | |
| "fmt" | |
| ) | |
| func main() { | |
| var s []int // nil slice | |
| t := []int{} // empty slice | |
| sj, _ := json.Marshal(s) | |
| tj, _ := json.Marshal(t) | |
| fmt.Printf("Nil slice as JSON: %s\n", sj) | |
| fmt.Printf("Empty slice as JSON: %s\n", tj) | |
| } |
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" | |
| func main() { | |
| // Create an array with three elements | |
| a := [3]int{1, 2, 3} | |
| // Create a slice from the first element of the array | |
| b := a[0:1] | |
| fmt.Printf("Slice b - Length: %d, Capacity: %d\n", len(b), cap(b)) // Output: Length: 1, Capacity: 3 | |
| } |
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" | |
| func main() { | |
| // Create an array with three elements | |
| a := [3]int{1, 2, 3} | |
| // Create a slice from the first element of the array | |
| b := a[0:1] | |
| // Create a slice from slice b | |
| c := b[0:2] | |
| fmt.Println("Slice c:", c) // Output: Slice c: [1 2] | |
| fmt.Printf("Slice c - Length: %d, Capacity: %d\n", len(c), cap(c)) // Output: Length: 2, Capacity: 3 | |
| } |
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" | |
| func main() { | |
| // Create an array with three elements | |
| a := [3]int{1, 2, 3} | |
| // Create a slice with length and capacity controlled by the three-index slice operator | |
| d := a[0:1:1] | |
| fmt.Println("Slice d:", d) // Output: Slice d: [1] | |
| fmt.Printf("Slice d - Length: %d, Capacity: %d\n", len(d), cap(d)) // Output: Length: 1, Capacity: 1 | |
| } |
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" | |
| func main() { | |
| // Create an array with three elements | |
| a := [3]int{1, 2, 3} | |
| // Create a slice from the first two elements of the array | |
| c := a[0:2] | |
| fmt.Println("Before append, Array a:", a) // Output: Before append, Array a: [1 2 3] | |
| fmt.Println("Before append, Slice c:", c) // Output: Before append, Slice c: [1 2] | |
| // Append to slice c | |
| c = append(c, 5) | |
| fmt.Println("After append, Array a:", a) // Output: After append, Array a: [1 2 5] | |
| fmt.Println("After append, Slice c:", c) // Output: After append, Slice c: [1 2 5] | |
| } |
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" | |
| func main() { | |
| // Create an array with three elements | |
| a := [3]int{1, 2, 3} | |
| // Create a slice with limited capacity | |
| c := a[0:2:2] | |
| fmt.Println("Before append, Array a:", a) // Output: Before append, Array a: [1 2 3] | |
| fmt.Println("Before append, Slice c:", c) // Output: Before append, Slice c: [1 2] | |
| // Append to slice c | |
| c = append(c, 5) | |
| fmt.Println("After append, Array a:", a) // Output: After append, Array a: [1 2 3] | |
| fmt.Println("After append, Slice c:", c) // Output: After append, Slice c: [1 2 5] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment