make और append से Slices बनाना
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
// 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)
}
Login to try C/C++/Java/PHP code in the editor
Appending Elements
append किसी slice के आखिर में एक या ज़्यादा elements जोड़ता है और resulting slice return करता है, जिसे आपको वापस किसी variable को assign करना चाहिए, क्योंकि append इस बात पर निर्भर करते हुए कि बढ़ने की जगह थी या नहीं वही underlying array return कर सकता है या नहीं।
उदाहरण: Appending Elements
// 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)
}
Login to try C/C++/Java/PHP code in the editor
Growing Beyond Capacity
जब append को current capacity की अनुमति से ज़्यादा जगह चाहिए, Go एक नई, बड़ी underlying array allocate करता है, मौजूदा elements को copy करता है, और नई array की ओर इशारा करती एक slice return करता है -- यही वजह है कि पुरानी और नई slice एक काफी बड़े append के बाद independent हो सकती हैं।
उदाहरण: Growing Beyond 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, 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))
}
}
Login to try C/C++/Java/PHP code in the editor
- append की return value को नज़रअंदाज़ करना, यह मानते हुए कि यह original slice variable को जगह पर modify करता है -- आपको result को reassign करना चाहिए।
- make([]int, 5) को एक खाली slice की उम्मीद में call करना, जबकि यह असल में zero values से भरी 5 length की एक slice बनाता है।
- किसी दूसरे 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: