Golang basis of an array of the basic exercise - Lee Chou

  1. Seeking the array [1, 3, 5, 7, 8] and all elements
    with code as follows:
package main
import "fmt"
func main()  {
	a := [...]int{1, 3, 5, 7, 8}
	sum := 0
	for _, v := range a {
		sum += v
	}
	fmt.Println(sum)
}
  1. Subscripts identify two elements in the array and the specified value of, for example, from the array [1, 3, 5, 7, 8] and to find the index of the two elements 8, respectively (0,3) and (1,2).
    With code as follows:
package main

import "fmt"

func getTwo(n int, arr ...int)  {
	for i := range arr{
		for j:=i+1; j<len(arr); j++ {
			if arr[i]+arr[j]==n {
				 fmt.Printf("(%d, %d)", i,j)
			}
		}
	}
}

func main()  {
	list := []int{1, 3, 5, 7, 8}
	a := 8
	getTwo(a, list...)  // 用切片来实现不定长数组的传参
}

Run shot:
Here Insert Picture Description
Note: The following section is sliced talk about the content, I am going to be understood as variable length arrays, it is an interface.
Levin followed Chou's blog to learn two or three days, not much time invested, but learn faster than watching video. I think that you have to learn in order to put those memories to make a few after-school exercise.
The question is simple, but for beginners learning outcomes is a good test, I also hope that these exercises recorded knowledge can learn something more solid, more hope to give beginners some of the other idea.
Written for: Array Li Zhou Go foundation of basic

Released four original articles · won praise 1 · views 116

Guess you like

Origin blog.csdn.net/qq_41580831/article/details/104049358