Misunderstanding the use of: = in Go

I was reading this document and saw the following snippet:

The syntax: = is shorthand for declaring and initializing a variable, for example. for var f string = "short" in this case.

f := "short"
fmt.Println(f)

      

Period: is it for strings only? Or dymanic enough to figure out what type of data should be stored?

And plus: isn't it the same as var f = "short"

?

+3


source to share


1 answer


Of course, this points to the obvious type (s) returned by the expression on the right side.

the spec gives the following examples:



i, j := 0, 10
f := func() int { return 7 }
ch := make(chan int)
r, w := os.Pipe(fd)  // os.Pipe() returns two values
_, y, _ := coord(p)  // coord() returns three values; only interested in y coordinate

      

Note that this is not dynamic: everything happens at compile time, the type is given by the expression on the right-hand side.

+5


source







All Articles