Today's Question:  What does your personal desk look like?        GIVE A SHOUT

 ALL


  Some tricks and tips for using for range in GoLang

GoLang provides two major ways to loop through elements of array, slice and map. They are for and for range. Many people find that for range is very convenient when don't care about the index of the element. In this post, some tricks and tips would be talked about regarding for range.1. Loop and get pointer of each elementAssume there is a code snippet like below which is to get the pointer of each element in an array and create a new array with the corresponding pointer.arr := [2]int{1, 2}res := []*int{}for _, v := range arr { res = append(res, &v)}//expect: 1 2fmt.Println(*res[0],*res...

29,107 0       FOR RANGE GOLANG FOR LOOP POINTER