Lessons
Go Introduction
Go Get Started
Go Syntax
Go Comments
Go Variables
- Declare Variables
- Go में Variables का परिचय
- Go में Multiple Variable Declaration
- Variable Naming Rules (नामकरण के नियम)
Go Constants
Go Output
Go Data Types
- Basic Data Types
- Go Boolean Data Type (बूलियन डेटा प्रकार)
- Go Integer Data Types
- Go Float Data Types
- Go String Data Type
Go Arrays
Go Slices
Go Operators
- Go Operators
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
Go Conditions
Go Switch
Go Loops
Go Functions
Go Struct
Go Maps
Project Structure
Slice के Elements Access करना
Slice के Elements Access करना
Slice के किसी भी element को index से access किया जा सकता है। Go में index हमेशा 0 से शुरू होता है।
package main
import "fmt"
func main() {
prices := []int{10, 20, 30}
fmt.Println(prices[0]) // First element
fmt.Println(prices[2]) // Third element
}
10 30
Slice के Elements Change करना
Slice के किसी element को बदलने के लिए index का उपयोग करें।
package main
import "fmt"
func main() {
prices := []int{10, 20, 30}
prices[2] = 50
fmt.Println(prices[0])
fmt.Println(prices[2])
}
10 50
Slice में Elements Append करना
append() function का use करके slice के end में elements जोड़ सकते हैं।
package main
import "fmt"
func main() {
myslice1 := []int{1,2,3,4,5,6}
fmt.Printf("myslice1 = %v\n", myslice1)
fmt.Printf("length = %d\n", len(myslice1))
fmt.Printf("capacity = %d\n", cap(myslice1))
myslice1 = append(myslice1, 20, 21)
fmt.Printf("myslice1 = %v\n", myslice1)
fmt.Printf("length = %d\n", len(myslice1))
fmt.Printf("capacity = %d\n", cap(myslice1))
}
myslice1 = [1 2 3 4 5 6] length = 6 capacity = 6 myslice1 = [1 2 3 4 5 6 20 21] length = 8 capacity = 12
One Slice को दूसरे में Append करना
slice2 के सभी elements को slice1 में जोड़ने के लिए append(slice1, slice2...) का उपयोग करें।
package main
import "fmt"
func main() {
myslice1 := []int{1,2,3}
myslice2 := []int{4,5,6}
myslice3 := append(myslice1, myslice2...)
fmt.Printf("myslice3=%v\n", myslice3)
fmt.Printf("length=%d\n", len(myslice3))
fmt.Printf("capacity=%d\n", cap(myslice3))
}
myslice3=[1 2 3 4 5 6] length=6 capacity=6
Slice की Length बदलना
Arrays के विपरीत, slice की length runtime में change की जा सकती है।
package main
import "fmt"
func main() {
arr1 := [6]int{9, 10, 11, 12, 13, 14}
myslice1 := arr1[1:5]
fmt.Printf("myslice1 = %v\n", myslice1)
fmt.Printf("length = %d\n", len(myslice1))
fmt.Printf("capacity = %d\n", cap(myslice1))
myslice1 = arr1[1:3]
fmt.Printf("myslice1 = %v\n", myslice1)
fmt.Printf("length = %d\n", len(myslice1))
fmt.Printf("capacity = %d\n", cap(myslice1))
myslice1 = append(myslice1, 20, 21, 22, 23)
fmt.Printf("myslice1 = %v\n", myslice1)
fmt.Printf("length = %d\n", len(myslice1))
fmt.Printf("capacity = %d\n", cap(myslice1))
}
myslice1 = [10 11 12 13] length = 4 capacity = 5 myslice1 = [10 11] length = 2 capacity = 5 myslice1 = [10 11 20 21 22 23] length = 6 capacity = 10
Memory Efficiency
यदि array बहुत बड़ा है और आपको केवल कुछ elements चाहिए, तो copy() function का use करना बेहतर है। यह memory बचाता है।
package main
import "fmt"
func main() {
numbers := []int{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}
fmt.Printf("numbers = %v\n", numbers)
fmt.Printf("length = %d\n", len(numbers))
fmt.Printf("capacity = %d\n", cap(numbers))
neededNumbers := numbers[:len(numbers)-10]
numbersCopy := make([]int, len(neededNumbers))
copy(numbersCopy, neededNumbers)
fmt.Printf("numbersCopy = %v\n", numbersCopy)
fmt.Printf("length = %d\n", len(numbersCopy))
fmt.Printf("capacity = %d\n", cap(numbersCopy))
}
// Original slice numbers = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15] length = 15 capacity = 15 // New slice numbersCopy = [1 2 3 4 5] length = 5 capacity = 5
copy() function original slice की जगह नया underlying array बनाता है, इसलिए memory efficiency बढ़ती है।