Slices क्या हैं
var name []type
name := []type{v1, v2, v3}
sub := name[low:high]
Creating a Slice
एक slice literal एक array literal जैसा दिखता है लेकिन बिना length के, जैसे []int{1, 2, 3}। आंतरिक रूप से यह एक underlying array की ओर इशारा करता एक छोटा structure है, एक length और capacity track करते हुए।
उदाहरण: Creating a Slice
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Short declaration: create nums and infer the type
nums := []int{10, 20, 30}
// Print the values followed by a newline
fmt.Println(nums, "len:", len(nums))
}
Login to try C/C++/Java/PHP code in the editor
Slicing Syntax
आप s[low:high] उपयोग करके किसी मौजूदा slice या array के हिस्से पर एक नया slice view बना सकते हैं, जो index low से लेकर (लेकिन high को शामिल किए बिना) elements चुनता है, एक जैसी underlying memory share करते हुए।
उदाहरण: Slicing Syntax
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Short declaration: create letters and infer the type
letters := []string{"a", "b", "c", "d", "e"}
// Short declaration: create middle and infer the type
middle := letters[1:4]
// Print the values followed by a newline
fmt.Println(middle)
}
Login to try C/C++/Java/PHP code in the editor
Length vs Capacity
किसी slice की length यह है कि यह वर्तमान में कितने elements रखता है; इसकी capacity यह है कि slice के पहले element से शुरू होकर underlying array कितने elements रख सकता है, जो यह मायने रखता है कि appending कैसे व्यवहार करता है।
उदाहरण: Length vs Capacity
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Short declaration: create s and infer the type
s := make([]int, 2, 5)
// Print the values followed by a newline
fmt.Println("len:", len(s), "cap:", cap(s))
}
Login to try C/C++/Java/PHP code in the editor
Shared Underlying Arrays
चूंकि एक slice किसी array पर एक view है, एक ही source से derived दो slices memory share कर सकते हैं -- एक slice के ज़रिए किसी element को modify करना दूसरे के ज़रिए दिखाई दे सकता है, जो सूक्ष्म bugs का एक आम स्रोत है।
उदाहरण: Shared Underlying Arrays
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Short declaration: create base and infer the type
base := []int{1, 2, 3, 4}
// Short declaration: create view and infer the type
view := base[1:3]
view[0] = 99
// Print the values followed by a newline
fmt.Println("base:", base)
}
Login to try C/C++/Java/PHP code in the editor
- किसी slice की length (len) को इसकी capacity (cap) के साथ confuse करना, boundary के पास append करते समय हैरानियों की ओर ले जाते हुए।
- यह मान लेना कि एक ही underlying array share करने वाले दो slices पूरी तरह independent हैं, फिर एक को modify करने पर दूसरा बदलने से confuse होना।
- 'var s []int' से एक slice बनाना और nil को एक error state की तरह treat करना, जबकि एक nil slice असल में पढ़ने और append करने के लिए बिल्कुल सुरक्षित है।
- एक slice किसी underlying array पर एक flexible, resizable view है।
- Slices की दोनों length (len) और capacity (cap) होती है।
- किसी मौजूदा array या slice को slice करना (s[low:high]) एक जैसा underlying data share करता है।
- एक nil slice की length 0 है और उपयोग के लिए सुरक्षित है -- यह कोई error state नहीं है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: