Currently there are two ways of declaring a variable in code:
x := 1
var x = 1
This is inconsistent. Every developer is free to use either style in their code
Proposal: have only one way of declaring variable, either var or :=
This proposal needs more background: what is the benefit that would outweigh the cost of updating ~all of the existing Go code?
Also note that the two forms are not entirely redundant today.
The :=
form can be used with unexported types:
package a
type token […]
func Generate() token { […] }
package b
func Snoop() {
t := a.Generate() // t has type a.token, but the name 'a.token' cannot be used explicitly within b.
[…]
}
The var
form can be used to promote a constant or a value of a literal type to an equivalent defined type (https://play.golang.org/p/LnfQ_voi-pS), while ensuring that the build will break if the value is not actually assignable — even if it could be converted explicitly (https://play.golang.org/p/rQLB7Sap3PA).
@bcmills
Wouldn't that also work with var as well
var t = a.Generate()
I think one of the things I like about Go is that there is typically only one way to write the same program (ie not having built-in streams). This proposal would break nearly every Go program, but it comes at the benefit of there being fewer ways to declare a variable. _If_ this goes through, since var
can do everything :=
can do (and more), we remove :=
.
Duplicate of #377? At least close enough.
That one seems more concerned about shadowing, and changing the :=
operator to fix that issue, this issue seems to be more around completely removing either :=
or var ...
so that there is only a single, consistent style to declare a variable.
I'm also in favor and think most core developers are but I think during Go1 design they actully tried really hard to do just that but they where stuck when it comes to pointer stuff, for example
https://play.golang.org/p/Uu9BQY9yNB1
package main
import (
"fmt"
)
func main() {
x := new(int)
var y *int
fmt.Println(x, y)
}
0x416020 <nil>
We need var
at top level because every top level statement starts with a keyword. Since we have var
at top level, we want it within functions.
We want :=
within functions because it is more concise.
In any case a change like is simply impossible to make at this point. It would break every existing Go program and invalidate all existing Go documentation and examples. There just isn't any benefit here that is worth that cost.
The :=
assignment operator is only allowed in a function scope. I imagine this was implemented because they want global variables to have to be explicitly initialized. Also, :=
is easy and shouldn't be removed for the sake of simplicity but there also needs to be a way to explicitly declare variables and variable types.
There're two completely different things: (variable declaration) and (same + assignment at once).
Completely useless and breaking change.
There're two completely different things: (variable declaration) and (same + assignment at once).
name := value
is the exact same as var name = value
.
I don't like this proposal too much (since it would, ya know, break nearly every Go program), but saying they are completely different is just not correct.
We definitely shouldn't change var
for package level variables. That should stay the same.
As for local variables, the main issue is that they both have exclusive benefits; var
allows you to easily get the zero value of a type, while :=
lets you do any other kind of assignment. If we were to remove var, we would have to make an easy way to get the zero value of a type with :=
; but at that point, instead of having two ways of declaring variables we have one way and a special case of that way.
I believe we should leave this as one of the quirks of Go, locking it down and throwing away the key as something that we shouldn't change.
while := lets you do any other kind of assignment
What do you mean by this?
@urandom Apologies for not being specific. I meant that :=
is optimized in terms of length and usability for most initialisations, like a := 50
, as opposed to var a = 50
.
I love the :=
syntax because it's short & simple.
I only use var
when explicitly needed and it is simply required in some cases as a type declaration.
The consistency benefits exist but they are not worth the huge cost.
I would go as far as to say I would stop using Go if var
became the only method of defining variables since the concise variable definition is such a core element of the language.
Thanks, but as I said above, we can't make this change to the language at this point.
Most helpful comment
We need
var
at top level because every top level statement starts with a keyword. Since we havevar
at top level, we want it within functions.We want
:=
within functions because it is more concise.In any case a change like is simply impossible to make at this point. It would break every existing Go program and invalidate all existing Go documentation and examples. There just isn't any benefit here that is worth that cost.