1. 程式人生 > >遍歷兩種方式比較

遍歷兩種方式比較

package main

import (
   "fmt"
)

// make
func main() {
   list := []int{0, 1, 2, 3}
   list = Test(list)
   fmt.Println(list)

   list1 := []int{0, 1, 2, 3}
   list1 = Test1(list1)
   fmt.Println(list1)
}

func Test(list []int) []int {
   for i := 0; i < len(list); i++ {
      list[i] += 1
   }
   fmt.Println("list:", list)
   return list
}

func Test1(list1 []int) []int {
   for _, j := range list1 {
      j += 1
   }
   fmt.Println("list:", list1)
   return list1
}