I. Problem description

Second, code demonstration

package main

import (
	"fmt"
	"strconv"
)

func twoSum(nums []int, target int) []int {
	var result []int
	for index, value := range (nums) {

		for i := 1; i < len(nums); i++ {
			if index < i{
			    if value+nums[i] == target {
			    	fmt.Println("["+strconv.Itoa(index)+","+strconv.Itoa(i)+"]")
			    	 result = []int{index,i}
		    	}
		    }
		}
	}
	return result
}

func main(a){
	var notes []int = []int{3.2.3}
	twoSum(notes,6)}Copy the code

Iii. Knowledge points

1. How arrays are defined

Arrays hold a specific number of elements and cannot grow or shrink. To declare a variable that holds an array, specify the number of elements it holds in square brackets ([]), followed by the type of element the array holds.

var myarry [4]string
Copy the code

2. Array literals

If you know in advance what values the array should hold, you can initialize the array with an array literal.

var myarry [4]string = [4]string{"a","b","c","d"}
Copy the code

Short variable declaration array

myarry := [4]string{"a","b","c","d"}
Copy the code

4. When an array is created, all the values it contains are initialized to zero values of the type the array holds.

package main

import "fmt"

func main(a){
	var myarry [5]int
	fmt.Println(myarry)
}

// Output is [0 0 0 0 0]

Copy the code

5. Loop through the number group

1) for… The range way

package main

import "fmt"

func main(a){
	var myarry [5]int = [5]int{1.2.3.4.5}
	for index,value := range myarry{
		fmt.Println(index,value)
	}
}

/ / print
/* 0, 1, 1, 2, 3, 3, 4, 4, 5 */
Copy the code

2) Blank identifiers in the for loop; When you do not need to print the value of a variable in a for loop, you can use the _ identifier.

Five, write in the back

Mr Lemon, senior operations engineer (self-described), SRE specialist (target), dreams of buying a Porsche by the age of 35. Like to study the bottom technology, think the bottom foundation is king. All new technologies are inseparable from the operating system (CPU, memory, disk), network, etc. Adhere to the input and output, record their own learning bit by bit, in the ordinary insist on moving forward, one day will meet a different yourself. Public account: Yunwewang (ID: Leeeee_Li).