← Back to Go Course | Chapter 5: Arrays Slices Maps | Lesson 2 of 7

Slices क्या हैं

एक slice एक flexible, expandable list जैसा है -- किसी array की lockers की fixed row के विपरीत, एक slice आपकी ज़रूरत जितने items रखने के लिए बढ़ सकता है।
Syntax
go
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

markup
// 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))
}

Slicing Syntax

आप s[low:high] उपयोग करके किसी मौजूदा slice या array के हिस्से पर एक नया slice view बना सकते हैं, जो index low से लेकर (लेकिन high को शामिल किए बिना) elements चुनता है, एक जैसी underlying memory share करते हुए।

उदाहरण: Slicing Syntax

markup
// 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)
}

Length vs Capacity

किसी slice की length यह है कि यह वर्तमान में कितने elements रखता है; इसकी capacity यह है कि slice के पहले element से शुरू होकर underlying array कितने elements रख सकता है, जो यह मायने रखता है कि appending कैसे व्यवहार करता है।

Note: cap(s) आपको बताता है कि एक नई underlying array allocate होने से पहले slice के पास बढ़ने के लिए कितनी जगह है।

उदाहरण: Length vs Capacity

markup
// 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))
}

Shared Underlying Arrays

चूंकि एक slice किसी array पर एक view है, एक ही source से derived दो slices memory share कर सकते हैं -- एक slice के ज़रिए किसी element को modify करना दूसरे के ज़रिए दिखाई दे सकता है, जो सूक्ष्म bugs का एक आम स्रोत है।

Note: जब आपको असल में एक independent slice चाहिए तो copy() या एक fresh slice पर append उपयोग करें।

उदाहरण: Shared Underlying Arrays

markup
// 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)
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. किसी slice की length (len) को इसकी capacity (cap) के साथ confuse करना, boundary के पास append करते समय हैरानियों की ओर ले जाते हुए।
  2. यह मान लेना कि एक ही underlying array share करने वाले दो slices पूरी तरह independent हैं, फिर एक को modify करने पर दूसरा बदलने से confuse होना।
  3. '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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.