โ€œThis is the 12th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.โ€

Write at the front ๐Ÿ‘€

Todayโ€™s focus is on arrays ๐ŸŒฏ

Array ๐Ÿฅซ

An Array is a fixed-length sequence of the same data type ๐Ÿ–

  1. A length is part of an array and must be an integer that cannot be changed once it is defined (hence arrays are rarely used directly in Go).
  2. Each element of an array can be accessed by index subscripts ranging from 0 to the point where the array length is minus 1len( )Functions andcap( )The array_length () function returns the length and capacity of an array

Define and initialize an array ๐Ÿ 

  • Grammar ๐Ÿ‘‡
Define an array:varArray name [array length] Type//Type indicates the data Type, which can be any basic TypeGlobal initialization:var a [3]bool = [3]bool{true.false}    // Uninitialized element values are the default values
    var b = [5]int{1.2.3.4.5}
    var c = [...]complex64{1+2i.2+1i.30.5 I.4+2i.50.1 I.6-5i}  / / [...]. Indicates that the array length is determined by the initialization value
    var d = [5]string{3: "Stubborn horns.".4: "LZH"} // Initialize elements with index numbers
    var e = [6]float64{3.14.3.141.3.1415}
    var f = [2]struct {
		name string
		age  uint} {{Bighorn bull.18}, {"Little Horned Ox".8}}  // The last line can be left out without a commaLocal initialization: g := [4]int{1.2}           
    h := [...]string{"Nami"."Sanji"."Zero"."Luffy"}   
    i := [9]bool{2: true.4:true} 
    j := [...]struct {
        book string
        price float64
    }{
        {"Go Programming Language".55.30 }, 
        {"Go Web Programming".60.83}, // The last line must have a comma
    }
Copy the code
  • Sample ๐Ÿ‘‡
package main

import "fmt"

var a [3]bool = [3]bool{true.false} // Uninitialized element values are the default values
var b = [5]int{1.2.3.4.5}
var c = [...]complex64{1 + 2i.2 + 1i.3 - 0.5 I.4 + 2i.5 - 0.1 I.6 - 5i} / / [...]. Indicates that the array length is determined by the initialization value
var d = [5]string{3: "Stubborn horns.".4: "LZH"}                                    // Initialize elements with index numbers
var e = [6]float64{3.14.3.141.3.1415}
var f = [2]struct {
	name string
	age  uint} {{Bighorn bull.18}, {"Little Horned Ox".8}} // The last line can be left out without a comma
func main(a) {
	g := [4]int{1.2}
	h := [...]string{"Nami"."Sanji"."Zero"."Luffy"}
	i := [9]bool{2: true.4: true}
	j := [...]struct {
		book  string
		price float64
	}{
		{"Go Programming Language".55.30},
		{"Go Web Programming".60.83}, // The last line must have a comma
	}
	fmt.Println(a, b, c, d, e, f)
	fmt.Println(g, h, i, j)
}

Copy the code
  • Results ๐Ÿ‘‡

Second, go through the number group ๐Ÿ—

1. Conventional methods ๐Ÿฅฉ

  • Sample ๐Ÿ‘‡
package main

import "fmt"

func main(a) {
	animal := [...]string{"The dog ๐Ÿฆฎ"."The kitten ๐Ÿˆ"."The pig ๐Ÿ–"."Little Rabbit ๐Ÿ‡"."Bighorn ox ๐Ÿ‚"}
	for index := 0; index < len(animal); index++ {
		fmt.Printf("animal[%d]=%s\n", index, animal[index])
	}
	fmt.Printf("There are %d animals.".len(animal))
}

Copy the code
  • Results ๐Ÿ‘‡

2. For range: ๐ŸฅŸ

  • Sample ๐Ÿ‘‡
package main

import "fmt"

func main(a) {
	animal := [...]string{"The dog ๐Ÿฆฎ"."The kitten ๐Ÿˆ"."The pig ๐Ÿ–"."Little Rabbit ๐Ÿ‡"."Bighorn ox ๐Ÿ‚"}
	for index, value := range animal {
		fmt.Printf("animal[%d]=%s\n", index, value)
	}
	fmt.Printf("There are %d animals.".len(animal))
}

Copy the code
  • Results ๐Ÿ‘‡

3. Arrays can be compared ๐Ÿฅ 

If both arrays are of the same length and type, you can compare the == and! Operators Can not compare two different types of arrays, otherwise the program will report an error ๐Ÿฅก

  • Sample ๐Ÿ‘‡
package main

import "fmt"

var a = [3]int{1.2.3}

func main(a) {
	b := [...]int{1.2.3}
	c := [3]int{}
	fmt.Println(a == b)
	fmt.Println(a == c)
	fmt.Println(b == c)
}

Copy the code
  • Results ๐Ÿ‘‡

Write it at the back ๐Ÿ™Œ

Thank you for watching โœจ. If you have any questions, please point them out to ๐Ÿ’–