hash := make(map[int]int) //key 存具体数字, value存索引 ret := [][2]int{}
sum := 40 for i := 0; i < len(arr); i++ { if _, ok := hash[sum-arr[i]]; ok { ret = append(ret, [2]int{hash[sum-arr[i]], i}) } else { hash[arr[i]] = i } }
for _, item := range ret { fmt.Printf("%d+%d=40\n", arr[item[0]], arr[item[1]]) }