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

make और append से Slices बनाना

make() आपको बढ़ने की जगह वाला एक खाली container बनाता है, और append() किसी slice में और items जोड़ने का तरीका है, आपको वापस (शायद resized) result देते हुए।
Syntax
go
name := make([]type, length, capacity)
name = append(name, value1, value2)
name = append(name, other...)

Creating a Slice with make

make([]Type, length) दी गई length की एक slice बनाता है, zero values से भरी, सीधे index करने के लिए तैयार। एक optional तीसरा argument initial capacity को length से अलग सेट करता है।

उदाहरण: Creating a Slice with make

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, 3)
	s[0], s[1], s[2] = 1, 2, 3
	// Print the values followed by a newline
	fmt.Println(s)
}

Appending Elements

append किसी slice के आखिर में एक या ज़्यादा elements जोड़ता है और resulting slice return करता है, जिसे आपको वापस किसी variable को assign करना चाहिए, क्योंकि append इस बात पर निर्भर करते हुए कि बढ़ने की जगह थी या नहीं वही underlying array return कर सकता है या नहीं।

Note: हमेशा 's = append(s, ...)' लिखें -- reassign करना भूल जाना सबसे आम Go beginner bugs में से एक है।

उदाहरण: Appending Elements

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{1, 2}
	// append adds elements and returns the updated slice
	nums = append(nums, 3, 4, 5)
	// Print the values followed by a newline
	fmt.Println(nums)
}

Growing Beyond Capacity

जब append को current capacity की अनुमति से ज़्यादा जगह चाहिए, Go एक नई, बड़ी underlying array allocate करता है, मौजूदा elements को copy करता है, और नई array की ओर इशारा करती एक slice return करता है -- यही वजह है कि पुरानी और नई slice एक काफी बड़े append के बाद independent हो सकती हैं।

उदाहरण: Growing Beyond 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, 0, 2)
	// Loop: repeat while the condition holds
	for i := 1; i <= 5; i++ {
		// append adds elements and returns the updated slice
		s = append(s, i)
		// Print the values followed by a newline
		fmt.Println("len:", len(s), "cap:", cap(s))
	}
}
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. append की return value को नज़रअंदाज़ करना, यह मानते हुए कि यह original slice variable को जगह पर modify करता है -- आपको result को reassign करना चाहिए।
  2. make([]int, 5) को एक खाली slice की उम्मीद में call करना, जबकि यह असल में zero values से भरी 5 length की एक slice बनाता है।
  3. किसी दूसरे slice को slice करके मिली slice में append करना और यह देखकर हैरान होना कि यह original की underlying array में data overwrite कर देता है।
चैप्टर सारांश
  • make([]Type, length, capacity) दिए गए size की एक नई slice allocate करता है।
  • append(slice, values...) elements जोड़ता है, एक (शायद नई) slice return करते हुए जिसे reassign किया जाना चाहिए।
  • अगर किसी slice की capacity exceed हो जाए, append अपने आप एक नई, बड़ी underlying array allocate करता है।
  • make(slice, 0, n) length 0 रखते हुए capacity pre-allocate करता है, तब उपयोगी जब final size लगभग ज्ञात हो।
🔒

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.