Slices को Copy करना
n := copy(dst, src)
Using copy()
built-in copy function एक source slice से elements को एक destination slice में copy करता है, और साधारण assignment के विपरीत, दोनों slices बाद में पूरी तरह अलग memory से backed हो जाते हैं।
उदाहरण: Using copy()
// 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)
}
Login to try C/C++/Java/PHP code in the editor
copy() Is Limited by the Shorter Slice
copy सिर्फ उतने elements transfer करता है जितने दोनों slices में से छोटे वाले में fit होते हैं -- अगर destination source से छोटा हो, अतिरिक्त source elements बस बिना किसी error के छोड़ दिए जाते हैं।
उदाहरण: copy() Is Limited by the Shorter 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 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)
}
Login to try C/C++/Java/PHP code in the editor
Independence After Copying
copy() चलने के बाद, destination slice को modify करना कभी source को प्रभावित नहीं करता, और उल्टा भी -- वे पूरी तरह अलग underlying arrays से backed हैं, जो assignment के बजाय copy उपयोग करने का पूरा मतलब है।
उदाहरण: Independence After Copying
// 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)
}
Login to try C/C++/Java/PHP code in the editor
- एक slice variable को दूसरे को assign करना ('b := a') एक independent copy की उम्मीद में, जबकि यह असल में बस एक जैसी underlying array share करता है।
- copy(dst, src) को dst के साथ src से कम elements के साथ call करना, और यह न समझना कि copy चुपचाप सिर्फ len(dst) तक copy करता है।
- यह भूल जाना कि 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: