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
Go Arrays
Go Arrays क्या हैं?
Arrays का उपयोग एक ही प्रकार के कई मानों को एक ही variable में संग्रहित करने के लिए किया जाता है।
Array Declare करने के तरीके
Go में array declare करने के दो तरीके हैं:
1. var keyword के साथ:
var array_name = [length]datatype{values} // length define
var array_name = [...]datatype{values} // length compiler द्वारा decide
2. := operator के साथ:
array_name := [length]datatype{values}
array_name := [...]datatype{values}
Array Examples
Defined Length Arrays:
package main
import "fmt"
func main() {
var arr1 = [3]int{1,2,3}
arr2 := [5]int{4,5,6,7,8}
fmt.Println(arr1)
fmt.Println(arr2)
}
[1 2 3]
[4 5 6 7 8]
Inferred Length Arrays:
package main
import "fmt"
func main() {
var arr1 = [...]int{1,2,3}
arr2 := [...]int{4,5,6,7,8}
fmt.Println(arr1)
fmt.Println(arr2)
}
[1 2 3]
[4 5 6 7 8]
String Array Example:
package main
import "fmt"
func main() {
var cars = [4]string{"Volvo", "BMW", "Ford", "Mazda"}
fmt.Print(cars)
}
[Volvo BMW Ford Mazda]
Array Elements Access करना
Array elements को index के द्वारा access किया जाता है। Go में index 0 से शुरू होता है।
package main
import "fmt"
func main() {
prices := [3]int{10,20,30}
fmt.Println(prices[0])
fmt.Println(prices[2])
}
10
30
Array Element Change करना
package main
import "fmt"
func main() {
prices := [3]int{10,20,30}
prices[2] = 50
fmt.Println(prices)
}
[10 20 50]
Array Initialization
अगर array या उसके elements initialize नहीं किए गए, तो default value assign होती है।
package main
import "fmt"
func main() {
arr1 := [5]int{} // not initialized
arr2 := [5]int{1,2} // partially initialized
arr3 := [5]int{1,2,3,4,5} // fully initialized
fmt.Println(arr1)
fmt.Println(arr2)
fmt.Println(arr3)
}
[0 0 0 0 0]
[1 2 0 0 0]
[1 2 3 4 5]
Specific Elements Initialize करना
package main
import "fmt"
func main() {
arr1 := [5]int{1:10, 2:40}
fmt.Println(arr1)
}
[0 10 40 0 0]
Array की Length जानना
package main
import "fmt"
func main() {
arr1 := [4]string{"Volvo","BMW","Ford","Mazda"}
arr2 := [...]int{1,2,3,4,5,6}
fmt.Println(len(arr1))
fmt.Println(len(arr2))
}
4
6