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

Slices को Copy करना

copy() किसी slice की contents की एक असली, अलग duplicate बनाता है, ताकि copy बदलना कभी original को न छुए।
Syntax
go
n := copy(dst, src)

Using copy()

built-in copy function एक source slice से elements को एक destination slice में copy करता है, और साधारण assignment के विपरीत, दोनों slices बाद में पूरी तरह अलग memory से backed हो जाते हैं।

उदाहरण: Using copy()

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 src and infer the type
	src := []int{1, 2, 3}
	// Short declaration: create dst and infer the type
	dst := make([]int, 3)
	n := copy(dst, src)
	// Print the values followed by a newline
	fmt.Println("copied:", n, "dst:", dst)
}

copy() Is Limited by the Shorter Slice

copy सिर्फ उतने elements transfer करता है जितने दोनों slices में से छोटे वाले में fit होते हैं -- अगर destination source से छोटा हो, अतिरिक्त source elements बस बिना किसी error के छोड़ दिए जाते हैं।

Note: copy करने से पहले सुनिश्चित करें कि destination slice कम से कम source जितना लंबा हो, ज़रूरत पड़ने पर make(len(src)) उपयोग करते हुए।

उदाहरण: copy() Is Limited by the Shorter 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 src and infer the type
	src := []int{1, 2, 3, 4, 5}
	// Short declaration: create dst and infer the type
	dst := make([]int, 2)
	n := copy(dst, src)
	// Print the values followed by a newline
	fmt.Println("copied:", n, "dst:", dst)
}

Independence After Copying

copy() चलने के बाद, destination slice को modify करना कभी source को प्रभावित नहीं करता, और उल्टा भी -- वे पूरी तरह अलग underlying arrays से backed हैं, जो assignment के बजाय copy उपयोग करने का पूरा मतलब है।

उदाहरण: Independence After Copying

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 original and infer the type
	original := []int{1, 2, 3}
	// Short declaration: create duplicate and infer the type
	duplicate := make([]int, len(original))
	copy(duplicate, original)
	duplicate[0] = 999
	// Print the values followed by a newline
	fmt.Println("original:", original)
	// Print the values followed by a newline
	fmt.Println("duplicate:", duplicate)
}
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 variable को दूसरे को assign करना ('b := a') एक independent copy की उम्मीद में, जबकि यह असल में बस एक जैसी underlying array share करता है।
  2. copy(dst, src) को dst के साथ src से कम elements के साथ call करना, और यह न समझना कि copy चुपचाप सिर्फ len(dst) तक copy करता है।
  3. यह भूल जाना कि copy() असल में copied elements की संख्या return करता है, जो एक पूरी copy हुई यह verify करने के लिए उपयोगी है।
चैप्टर सारांश
  • copy(dst, src) src से dst में elements copy करता है, दोनों में से छोटे की length तक।
  • copy() एक genuinely independent slice बनाने का तरीका है, साधारण assignment के विपरीत।
  • Copy किए elements की संख्या दोनों में से जो भी slice (dst या src) छोटी हो उससे सीमित है।
  • copy() एक int return करता है: असल में कितने elements copy हुए।
🔒

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.