Monday, 11 April 2016

go / golang difference between := vs =

:= is for declaration + assignment, and = is for assignment only
For the golang documentation:

Inside a function, the := short assignment statement can be used in place of a var declaration with implicit type.

Outside a function, every construct begins with a keyword (var, func, and so on) and the := construct is not available.

In the below example:
the usage of both := and  = operator can be seen.

func Reverse(s string) string {
 r := []rune(s)
 for i,j := 0,len(r)-1 ; i < len(r)/2 ; i,j=i+1,j-1 {
  r[i],r[j] = r[j],r[i]
 }
 return string(r)
}


No comments:

Post a Comment