Difference between := and = operators in Go
In Go, what is the difference between the =
and :=
operator? They both seem to be for assignment? This might be obvious but I can't seem to find it in the docs.
go colon-equals
add a comment |
In Go, what is the difference between the =
and :=
operator? They both seem to be for assignment? This might be obvious but I can't seem to find it in the docs.
go colon-equals
1
Also see this: Go Variables Visual Guide. I wrote an article about it.
– Inanc Gumus
Nov 20 '17 at 13:39
1
The semantics...
– JustDave
Jan 27 '18 at 15:27
add a comment |
In Go, what is the difference between the =
and :=
operator? They both seem to be for assignment? This might be obvious but I can't seem to find it in the docs.
go colon-equals
In Go, what is the difference between the =
and :=
operator? They both seem to be for assignment? This might be obvious but I can't seem to find it in the docs.
go colon-equals
go colon-equals
edited Nov 13 '18 at 13:10
Moody_Mudskipper
21.8k32864
21.8k32864
asked Jul 26 '13 at 21:20
ChrisChris
3,04362544
3,04362544
1
Also see this: Go Variables Visual Guide. I wrote an article about it.
– Inanc Gumus
Nov 20 '17 at 13:39
1
The semantics...
– JustDave
Jan 27 '18 at 15:27
add a comment |
1
Also see this: Go Variables Visual Guide. I wrote an article about it.
– Inanc Gumus
Nov 20 '17 at 13:39
1
The semantics...
– JustDave
Jan 27 '18 at 15:27
1
1
Also see this: Go Variables Visual Guide. I wrote an article about it.
– Inanc Gumus
Nov 20 '17 at 13:39
Also see this: Go Variables Visual Guide. I wrote an article about it.
– Inanc Gumus
Nov 20 '17 at 13:39
1
1
The semantics...
– JustDave
Jan 27 '18 at 15:27
The semantics...
– JustDave
Jan 27 '18 at 15:27
add a comment |
9 Answers
9
active
oldest
votes
Only =
is the assignment operator.
:=
is actually not an operator at all. It is a part of the syntax of the Short variable declarations clause.
so does it mean variable with inferred type? right?
– Krupal Shah
May 15 '16 at 11:49
@KrupalShah the link to the docs literally says that - "It is shorthand for a regular variable declaration with initializer expressions but no types:" golang.org/ref/spec#Short_variable_declarations
– akshaynagpal
Jun 16 '18 at 20:18
Looks like:=
is listed as an operator here golang.org/ref/spec#Operators_and_punctuation, so I'm not sure I agree that ":=
is actually not an operator"
– Powers
Jun 25 '18 at 2:57
add a comment |
In Go, :=
is for declaration + assignment, whereas =
is for assignment only.
For example, var foo int = 10
is the same as foo := 10
.
31
This should be the answer. Examples are so much more useful than just a link to the documentation.
– kemicofa
Jan 11 '17 at 14:29
1
Is there a use case for=
as opposed to:=
? Should you just always use:=
?
– Kenneth Worden
Jul 27 '17 at 15:45
@rottenoats I agree and I've just added more examples in my answer.
– Inanc Gumus
Aug 13 '17 at 19:23
1
@KennethWorden Go won't let you use:=
to assign to a variable that has already been declared, unless you are assigning to multiple variables at once, and at least one of those variables is new.
– Kenny Bania
Aug 30 '17 at 18:36
theint
is not required,var foo = 10
is the same asfoo := 10
– Gary Lyn
Nov 21 '17 at 14:04
add a comment |
As others have explained already, :=
is for both declaration, assignment, and also for redeclaration; and it guesses (infer) the variable's type automatically.
It's a short-hand form of:
var foo int
foo = 32
// OR:
var foo int = 32
// OR:
var foo = 32
Some rules
You can't use :=
out of funcs
. It's because, out of any func, a statement should start with a keyword.
illegal := 42
func foo()
legal := 42
You can't use them twice (in the same scope):
legal := 42
legal := 42 // <-- error
Because, :=
introduces "a new variable", hence using it twice does not redeclare a second variable, so it's illegal.
However, you can use them twice in "multi-variable" declarations, if one of the variables is new:
foo, bar := someFunc()
foo, jazz := someFunc() // <-- jazz is new
baz, foo := someFunc() // <-- baz is new
This is legal, because, you're not declaring all the variables, you're just reassigning new values to the existing variables, and declaring new variables at the same time.
You can use the short declaration to declare a variable in a newer scope even that variable is already declared with the same name before:
var foo int = 34
func some()
// because foo here is scoped to some func
foo := 42 // <-- legal
foo = 314 // <-- legal
Here, foo := 42
is legal, because, it declares foo
in some()
func's scope. foo = 314
is legal, because, it just assigns a new value to foo
.
You can use them for multi-variable declarations and assignments:
foo, bar := 42, 314
jazz, bazz := 22, 7
You can declare the same name in short statement blocks like: if, for, switch:
foo := 42
if foo := someFunc(); foo == 314
// foo is scoped to 314 here
// ...
// foo is still 42 here
Because, foo
in if foo := ...
, only belongs to that if
clause and it's in a different scope.
So, as a general rule: If you want to easily declare a variable you can use :=
, or, if you want to overwrite an existing value, you can use =
.
2
Thanks for mentioning all the use cases.
– AnirbanDebnath
Jan 28 '18 at 4:43
add a comment |
:=
is a short-hand for declaration.
a := 10
b := "gopher"
a
will be declared as an int
and initialized with value 10
where as b
will be declared as a string and initialized with value gopher
.
Their equivalents using =
would be
var a = 10
var b = "gopher"
=
is assignment operator. It is used the same way you would use it in any other language.
You can omit the type when you declare the variable and an initializer is present (http://tour.golang.org/#11).
add a comment |
:=
declares and assigns, =
just assigns
It's useful when you don't want to fill up your code with type or struct declarations.
// Usage with =
var i int
var U, V, W float64
var k = 0
var x, y float32 = -1, -2
// Usage with :=
i, j := 0, 10
f := func() int return 7
ch := make(chan int)
add a comment |
The := means declare and assign while the = means to simply assign.
add a comment |
from the reference doc : (tour.golang.org)
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.
add a comment |
“:=” use to do declaration and initilization at the same time, following is an example.
Usage of “=”
var i int
i = 10
https://play.golang.org/p/RU88ty_SGa
Usage of “:=”
i := 10
https://play.golang.org/p/XBdjBh-DQB
add a comment |
Both are the different technique of variable declaration in Go language.
var firstName := "John" // is a variable declaration
AND
firstName := "John" // is a short variable declaration.
A short variable declaration is a shorthand for a regular variable declaration with initializer expressions but no types.
Read below for detail:
Variable declarations
Short variable declarations
9
I think your syntax is wrong for the first example. Instead of: var firstName := "John" , it should be: var firstName = "John"
– Gino
May 20 '16 at 14:10
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17891226%2fdifference-between-and-operators-in-go%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
Only =
is the assignment operator.
:=
is actually not an operator at all. It is a part of the syntax of the Short variable declarations clause.
so does it mean variable with inferred type? right?
– Krupal Shah
May 15 '16 at 11:49
@KrupalShah the link to the docs literally says that - "It is shorthand for a regular variable declaration with initializer expressions but no types:" golang.org/ref/spec#Short_variable_declarations
– akshaynagpal
Jun 16 '18 at 20:18
Looks like:=
is listed as an operator here golang.org/ref/spec#Operators_and_punctuation, so I'm not sure I agree that ":=
is actually not an operator"
– Powers
Jun 25 '18 at 2:57
add a comment |
Only =
is the assignment operator.
:=
is actually not an operator at all. It is a part of the syntax of the Short variable declarations clause.
so does it mean variable with inferred type? right?
– Krupal Shah
May 15 '16 at 11:49
@KrupalShah the link to the docs literally says that - "It is shorthand for a regular variable declaration with initializer expressions but no types:" golang.org/ref/spec#Short_variable_declarations
– akshaynagpal
Jun 16 '18 at 20:18
Looks like:=
is listed as an operator here golang.org/ref/spec#Operators_and_punctuation, so I'm not sure I agree that ":=
is actually not an operator"
– Powers
Jun 25 '18 at 2:57
add a comment |
Only =
is the assignment operator.
:=
is actually not an operator at all. It is a part of the syntax of the Short variable declarations clause.
Only =
is the assignment operator.
:=
is actually not an operator at all. It is a part of the syntax of the Short variable declarations clause.
answered Jul 26 '13 at 21:25
zzzzzzzz
53.2k12130114
53.2k12130114
so does it mean variable with inferred type? right?
– Krupal Shah
May 15 '16 at 11:49
@KrupalShah the link to the docs literally says that - "It is shorthand for a regular variable declaration with initializer expressions but no types:" golang.org/ref/spec#Short_variable_declarations
– akshaynagpal
Jun 16 '18 at 20:18
Looks like:=
is listed as an operator here golang.org/ref/spec#Operators_and_punctuation, so I'm not sure I agree that ":=
is actually not an operator"
– Powers
Jun 25 '18 at 2:57
add a comment |
so does it mean variable with inferred type? right?
– Krupal Shah
May 15 '16 at 11:49
@KrupalShah the link to the docs literally says that - "It is shorthand for a regular variable declaration with initializer expressions but no types:" golang.org/ref/spec#Short_variable_declarations
– akshaynagpal
Jun 16 '18 at 20:18
Looks like:=
is listed as an operator here golang.org/ref/spec#Operators_and_punctuation, so I'm not sure I agree that ":=
is actually not an operator"
– Powers
Jun 25 '18 at 2:57
so does it mean variable with inferred type? right?
– Krupal Shah
May 15 '16 at 11:49
so does it mean variable with inferred type? right?
– Krupal Shah
May 15 '16 at 11:49
@KrupalShah the link to the docs literally says that - "It is shorthand for a regular variable declaration with initializer expressions but no types:" golang.org/ref/spec#Short_variable_declarations
– akshaynagpal
Jun 16 '18 at 20:18
@KrupalShah the link to the docs literally says that - "It is shorthand for a regular variable declaration with initializer expressions but no types:" golang.org/ref/spec#Short_variable_declarations
– akshaynagpal
Jun 16 '18 at 20:18
Looks like
:=
is listed as an operator here golang.org/ref/spec#Operators_and_punctuation, so I'm not sure I agree that ":=
is actually not an operator"– Powers
Jun 25 '18 at 2:57
Looks like
:=
is listed as an operator here golang.org/ref/spec#Operators_and_punctuation, so I'm not sure I agree that ":=
is actually not an operator"– Powers
Jun 25 '18 at 2:57
add a comment |
In Go, :=
is for declaration + assignment, whereas =
is for assignment only.
For example, var foo int = 10
is the same as foo := 10
.
31
This should be the answer. Examples are so much more useful than just a link to the documentation.
– kemicofa
Jan 11 '17 at 14:29
1
Is there a use case for=
as opposed to:=
? Should you just always use:=
?
– Kenneth Worden
Jul 27 '17 at 15:45
@rottenoats I agree and I've just added more examples in my answer.
– Inanc Gumus
Aug 13 '17 at 19:23
1
@KennethWorden Go won't let you use:=
to assign to a variable that has already been declared, unless you are assigning to multiple variables at once, and at least one of those variables is new.
– Kenny Bania
Aug 30 '17 at 18:36
theint
is not required,var foo = 10
is the same asfoo := 10
– Gary Lyn
Nov 21 '17 at 14:04
add a comment |
In Go, :=
is for declaration + assignment, whereas =
is for assignment only.
For example, var foo int = 10
is the same as foo := 10
.
31
This should be the answer. Examples are so much more useful than just a link to the documentation.
– kemicofa
Jan 11 '17 at 14:29
1
Is there a use case for=
as opposed to:=
? Should you just always use:=
?
– Kenneth Worden
Jul 27 '17 at 15:45
@rottenoats I agree and I've just added more examples in my answer.
– Inanc Gumus
Aug 13 '17 at 19:23
1
@KennethWorden Go won't let you use:=
to assign to a variable that has already been declared, unless you are assigning to multiple variables at once, and at least one of those variables is new.
– Kenny Bania
Aug 30 '17 at 18:36
theint
is not required,var foo = 10
is the same asfoo := 10
– Gary Lyn
Nov 21 '17 at 14:04
add a comment |
In Go, :=
is for declaration + assignment, whereas =
is for assignment only.
For example, var foo int = 10
is the same as foo := 10
.
In Go, :=
is for declaration + assignment, whereas =
is for assignment only.
For example, var foo int = 10
is the same as foo := 10
.
edited Aug 20 '17 at 16:30
Ricardo Stuven
3,31712532
3,31712532
answered Jul 26 '13 at 21:26
ChaosChaos
6,307123363
6,307123363
31
This should be the answer. Examples are so much more useful than just a link to the documentation.
– kemicofa
Jan 11 '17 at 14:29
1
Is there a use case for=
as opposed to:=
? Should you just always use:=
?
– Kenneth Worden
Jul 27 '17 at 15:45
@rottenoats I agree and I've just added more examples in my answer.
– Inanc Gumus
Aug 13 '17 at 19:23
1
@KennethWorden Go won't let you use:=
to assign to a variable that has already been declared, unless you are assigning to multiple variables at once, and at least one of those variables is new.
– Kenny Bania
Aug 30 '17 at 18:36
theint
is not required,var foo = 10
is the same asfoo := 10
– Gary Lyn
Nov 21 '17 at 14:04
add a comment |
31
This should be the answer. Examples are so much more useful than just a link to the documentation.
– kemicofa
Jan 11 '17 at 14:29
1
Is there a use case for=
as opposed to:=
? Should you just always use:=
?
– Kenneth Worden
Jul 27 '17 at 15:45
@rottenoats I agree and I've just added more examples in my answer.
– Inanc Gumus
Aug 13 '17 at 19:23
1
@KennethWorden Go won't let you use:=
to assign to a variable that has already been declared, unless you are assigning to multiple variables at once, and at least one of those variables is new.
– Kenny Bania
Aug 30 '17 at 18:36
theint
is not required,var foo = 10
is the same asfoo := 10
– Gary Lyn
Nov 21 '17 at 14:04
31
31
This should be the answer. Examples are so much more useful than just a link to the documentation.
– kemicofa
Jan 11 '17 at 14:29
This should be the answer. Examples are so much more useful than just a link to the documentation.
– kemicofa
Jan 11 '17 at 14:29
1
1
Is there a use case for
=
as opposed to :=
? Should you just always use :=
?– Kenneth Worden
Jul 27 '17 at 15:45
Is there a use case for
=
as opposed to :=
? Should you just always use :=
?– Kenneth Worden
Jul 27 '17 at 15:45
@rottenoats I agree and I've just added more examples in my answer.
– Inanc Gumus
Aug 13 '17 at 19:23
@rottenoats I agree and I've just added more examples in my answer.
– Inanc Gumus
Aug 13 '17 at 19:23
1
1
@KennethWorden Go won't let you use
:=
to assign to a variable that has already been declared, unless you are assigning to multiple variables at once, and at least one of those variables is new.– Kenny Bania
Aug 30 '17 at 18:36
@KennethWorden Go won't let you use
:=
to assign to a variable that has already been declared, unless you are assigning to multiple variables at once, and at least one of those variables is new.– Kenny Bania
Aug 30 '17 at 18:36
the
int
is not required, var foo = 10
is the same as foo := 10
– Gary Lyn
Nov 21 '17 at 14:04
the
int
is not required, var foo = 10
is the same as foo := 10
– Gary Lyn
Nov 21 '17 at 14:04
add a comment |
As others have explained already, :=
is for both declaration, assignment, and also for redeclaration; and it guesses (infer) the variable's type automatically.
It's a short-hand form of:
var foo int
foo = 32
// OR:
var foo int = 32
// OR:
var foo = 32
Some rules
You can't use :=
out of funcs
. It's because, out of any func, a statement should start with a keyword.
illegal := 42
func foo()
legal := 42
You can't use them twice (in the same scope):
legal := 42
legal := 42 // <-- error
Because, :=
introduces "a new variable", hence using it twice does not redeclare a second variable, so it's illegal.
However, you can use them twice in "multi-variable" declarations, if one of the variables is new:
foo, bar := someFunc()
foo, jazz := someFunc() // <-- jazz is new
baz, foo := someFunc() // <-- baz is new
This is legal, because, you're not declaring all the variables, you're just reassigning new values to the existing variables, and declaring new variables at the same time.
You can use the short declaration to declare a variable in a newer scope even that variable is already declared with the same name before:
var foo int = 34
func some()
// because foo here is scoped to some func
foo := 42 // <-- legal
foo = 314 // <-- legal
Here, foo := 42
is legal, because, it declares foo
in some()
func's scope. foo = 314
is legal, because, it just assigns a new value to foo
.
You can use them for multi-variable declarations and assignments:
foo, bar := 42, 314
jazz, bazz := 22, 7
You can declare the same name in short statement blocks like: if, for, switch:
foo := 42
if foo := someFunc(); foo == 314
// foo is scoped to 314 here
// ...
// foo is still 42 here
Because, foo
in if foo := ...
, only belongs to that if
clause and it's in a different scope.
So, as a general rule: If you want to easily declare a variable you can use :=
, or, if you want to overwrite an existing value, you can use =
.
2
Thanks for mentioning all the use cases.
– AnirbanDebnath
Jan 28 '18 at 4:43
add a comment |
As others have explained already, :=
is for both declaration, assignment, and also for redeclaration; and it guesses (infer) the variable's type automatically.
It's a short-hand form of:
var foo int
foo = 32
// OR:
var foo int = 32
// OR:
var foo = 32
Some rules
You can't use :=
out of funcs
. It's because, out of any func, a statement should start with a keyword.
illegal := 42
func foo()
legal := 42
You can't use them twice (in the same scope):
legal := 42
legal := 42 // <-- error
Because, :=
introduces "a new variable", hence using it twice does not redeclare a second variable, so it's illegal.
However, you can use them twice in "multi-variable" declarations, if one of the variables is new:
foo, bar := someFunc()
foo, jazz := someFunc() // <-- jazz is new
baz, foo := someFunc() // <-- baz is new
This is legal, because, you're not declaring all the variables, you're just reassigning new values to the existing variables, and declaring new variables at the same time.
You can use the short declaration to declare a variable in a newer scope even that variable is already declared with the same name before:
var foo int = 34
func some()
// because foo here is scoped to some func
foo := 42 // <-- legal
foo = 314 // <-- legal
Here, foo := 42
is legal, because, it declares foo
in some()
func's scope. foo = 314
is legal, because, it just assigns a new value to foo
.
You can use them for multi-variable declarations and assignments:
foo, bar := 42, 314
jazz, bazz := 22, 7
You can declare the same name in short statement blocks like: if, for, switch:
foo := 42
if foo := someFunc(); foo == 314
// foo is scoped to 314 here
// ...
// foo is still 42 here
Because, foo
in if foo := ...
, only belongs to that if
clause and it's in a different scope.
So, as a general rule: If you want to easily declare a variable you can use :=
, or, if you want to overwrite an existing value, you can use =
.
2
Thanks for mentioning all the use cases.
– AnirbanDebnath
Jan 28 '18 at 4:43
add a comment |
As others have explained already, :=
is for both declaration, assignment, and also for redeclaration; and it guesses (infer) the variable's type automatically.
It's a short-hand form of:
var foo int
foo = 32
// OR:
var foo int = 32
// OR:
var foo = 32
Some rules
You can't use :=
out of funcs
. It's because, out of any func, a statement should start with a keyword.
illegal := 42
func foo()
legal := 42
You can't use them twice (in the same scope):
legal := 42
legal := 42 // <-- error
Because, :=
introduces "a new variable", hence using it twice does not redeclare a second variable, so it's illegal.
However, you can use them twice in "multi-variable" declarations, if one of the variables is new:
foo, bar := someFunc()
foo, jazz := someFunc() // <-- jazz is new
baz, foo := someFunc() // <-- baz is new
This is legal, because, you're not declaring all the variables, you're just reassigning new values to the existing variables, and declaring new variables at the same time.
You can use the short declaration to declare a variable in a newer scope even that variable is already declared with the same name before:
var foo int = 34
func some()
// because foo here is scoped to some func
foo := 42 // <-- legal
foo = 314 // <-- legal
Here, foo := 42
is legal, because, it declares foo
in some()
func's scope. foo = 314
is legal, because, it just assigns a new value to foo
.
You can use them for multi-variable declarations and assignments:
foo, bar := 42, 314
jazz, bazz := 22, 7
You can declare the same name in short statement blocks like: if, for, switch:
foo := 42
if foo := someFunc(); foo == 314
// foo is scoped to 314 here
// ...
// foo is still 42 here
Because, foo
in if foo := ...
, only belongs to that if
clause and it's in a different scope.
So, as a general rule: If you want to easily declare a variable you can use :=
, or, if you want to overwrite an existing value, you can use =
.
As others have explained already, :=
is for both declaration, assignment, and also for redeclaration; and it guesses (infer) the variable's type automatically.
It's a short-hand form of:
var foo int
foo = 32
// OR:
var foo int = 32
// OR:
var foo = 32
Some rules
You can't use :=
out of funcs
. It's because, out of any func, a statement should start with a keyword.
illegal := 42
func foo()
legal := 42
You can't use them twice (in the same scope):
legal := 42
legal := 42 // <-- error
Because, :=
introduces "a new variable", hence using it twice does not redeclare a second variable, so it's illegal.
However, you can use them twice in "multi-variable" declarations, if one of the variables is new:
foo, bar := someFunc()
foo, jazz := someFunc() // <-- jazz is new
baz, foo := someFunc() // <-- baz is new
This is legal, because, you're not declaring all the variables, you're just reassigning new values to the existing variables, and declaring new variables at the same time.
You can use the short declaration to declare a variable in a newer scope even that variable is already declared with the same name before:
var foo int = 34
func some()
// because foo here is scoped to some func
foo := 42 // <-- legal
foo = 314 // <-- legal
Here, foo := 42
is legal, because, it declares foo
in some()
func's scope. foo = 314
is legal, because, it just assigns a new value to foo
.
You can use them for multi-variable declarations and assignments:
foo, bar := 42, 314
jazz, bazz := 22, 7
You can declare the same name in short statement blocks like: if, for, switch:
foo := 42
if foo := someFunc(); foo == 314
// foo is scoped to 314 here
// ...
// foo is still 42 here
Because, foo
in if foo := ...
, only belongs to that if
clause and it's in a different scope.
So, as a general rule: If you want to easily declare a variable you can use :=
, or, if you want to overwrite an existing value, you can use =
.
edited Jan 10 at 13:07
answered Aug 12 '17 at 19:38
Inanc GumusInanc Gumus
5,99234051
5,99234051
2
Thanks for mentioning all the use cases.
– AnirbanDebnath
Jan 28 '18 at 4:43
add a comment |
2
Thanks for mentioning all the use cases.
– AnirbanDebnath
Jan 28 '18 at 4:43
2
2
Thanks for mentioning all the use cases.
– AnirbanDebnath
Jan 28 '18 at 4:43
Thanks for mentioning all the use cases.
– AnirbanDebnath
Jan 28 '18 at 4:43
add a comment |
:=
is a short-hand for declaration.
a := 10
b := "gopher"
a
will be declared as an int
and initialized with value 10
where as b
will be declared as a string and initialized with value gopher
.
Their equivalents using =
would be
var a = 10
var b = "gopher"
=
is assignment operator. It is used the same way you would use it in any other language.
You can omit the type when you declare the variable and an initializer is present (http://tour.golang.org/#11).
add a comment |
:=
is a short-hand for declaration.
a := 10
b := "gopher"
a
will be declared as an int
and initialized with value 10
where as b
will be declared as a string and initialized with value gopher
.
Their equivalents using =
would be
var a = 10
var b = "gopher"
=
is assignment operator. It is used the same way you would use it in any other language.
You can omit the type when you declare the variable and an initializer is present (http://tour.golang.org/#11).
add a comment |
:=
is a short-hand for declaration.
a := 10
b := "gopher"
a
will be declared as an int
and initialized with value 10
where as b
will be declared as a string and initialized with value gopher
.
Their equivalents using =
would be
var a = 10
var b = "gopher"
=
is assignment operator. It is used the same way you would use it in any other language.
You can omit the type when you declare the variable and an initializer is present (http://tour.golang.org/#11).
:=
is a short-hand for declaration.
a := 10
b := "gopher"
a
will be declared as an int
and initialized with value 10
where as b
will be declared as a string and initialized with value gopher
.
Their equivalents using =
would be
var a = 10
var b = "gopher"
=
is assignment operator. It is used the same way you would use it in any other language.
You can omit the type when you declare the variable and an initializer is present (http://tour.golang.org/#11).
edited Jul 27 '13 at 6:19
answered Jul 27 '13 at 6:03
ShuklaSannidhyaShuklaSannidhya
3,86842443
3,86842443
add a comment |
add a comment |
:=
declares and assigns, =
just assigns
It's useful when you don't want to fill up your code with type or struct declarations.
// Usage with =
var i int
var U, V, W float64
var k = 0
var x, y float32 = -1, -2
// Usage with :=
i, j := 0, 10
f := func() int return 7
ch := make(chan int)
add a comment |
:=
declares and assigns, =
just assigns
It's useful when you don't want to fill up your code with type or struct declarations.
// Usage with =
var i int
var U, V, W float64
var k = 0
var x, y float32 = -1, -2
// Usage with :=
i, j := 0, 10
f := func() int return 7
ch := make(chan int)
add a comment |
:=
declares and assigns, =
just assigns
It's useful when you don't want to fill up your code with type or struct declarations.
// Usage with =
var i int
var U, V, W float64
var k = 0
var x, y float32 = -1, -2
// Usage with :=
i, j := 0, 10
f := func() int return 7
ch := make(chan int)
:=
declares and assigns, =
just assigns
It's useful when you don't want to fill up your code with type or struct declarations.
// Usage with =
var i int
var U, V, W float64
var k = 0
var x, y float32 = -1, -2
// Usage with :=
i, j := 0, 10
f := func() int return 7
ch := make(chan int)
answered Jul 26 '13 at 21:28
GustavGustav
2,29311725
2,29311725
add a comment |
add a comment |
The := means declare and assign while the = means to simply assign.
add a comment |
The := means declare and assign while the = means to simply assign.
add a comment |
The := means declare and assign while the = means to simply assign.
The := means declare and assign while the = means to simply assign.
answered Jul 26 '13 at 21:25
Ralph CaraveoRalph Caraveo
5,89753045
5,89753045
add a comment |
add a comment |
from the reference doc : (tour.golang.org)
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.
add a comment |
from the reference doc : (tour.golang.org)
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.
add a comment |
from the reference doc : (tour.golang.org)
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.
from the reference doc : (tour.golang.org)
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.
answered Aug 23 '13 at 8:05
subhash kumar singhsubhash kumar singh
1,43842539
1,43842539
add a comment |
add a comment |
“:=” use to do declaration and initilization at the same time, following is an example.
Usage of “=”
var i int
i = 10
https://play.golang.org/p/RU88ty_SGa
Usage of “:=”
i := 10
https://play.golang.org/p/XBdjBh-DQB
add a comment |
“:=” use to do declaration and initilization at the same time, following is an example.
Usage of “=”
var i int
i = 10
https://play.golang.org/p/RU88ty_SGa
Usage of “:=”
i := 10
https://play.golang.org/p/XBdjBh-DQB
add a comment |
“:=” use to do declaration and initilization at the same time, following is an example.
Usage of “=”
var i int
i = 10
https://play.golang.org/p/RU88ty_SGa
Usage of “:=”
i := 10
https://play.golang.org/p/XBdjBh-DQB
“:=” use to do declaration and initilization at the same time, following is an example.
Usage of “=”
var i int
i = 10
https://play.golang.org/p/RU88ty_SGa
Usage of “:=”
i := 10
https://play.golang.org/p/XBdjBh-DQB
answered Mar 16 '17 at 4:22
Nisal EduNisal Edu
2,89021423
2,89021423
add a comment |
add a comment |
Both are the different technique of variable declaration in Go language.
var firstName := "John" // is a variable declaration
AND
firstName := "John" // is a short variable declaration.
A short variable declaration is a shorthand for a regular variable declaration with initializer expressions but no types.
Read below for detail:
Variable declarations
Short variable declarations
9
I think your syntax is wrong for the first example. Instead of: var firstName := "John" , it should be: var firstName = "John"
– Gino
May 20 '16 at 14:10
add a comment |
Both are the different technique of variable declaration in Go language.
var firstName := "John" // is a variable declaration
AND
firstName := "John" // is a short variable declaration.
A short variable declaration is a shorthand for a regular variable declaration with initializer expressions but no types.
Read below for detail:
Variable declarations
Short variable declarations
9
I think your syntax is wrong for the first example. Instead of: var firstName := "John" , it should be: var firstName = "John"
– Gino
May 20 '16 at 14:10
add a comment |
Both are the different technique of variable declaration in Go language.
var firstName := "John" // is a variable declaration
AND
firstName := "John" // is a short variable declaration.
A short variable declaration is a shorthand for a regular variable declaration with initializer expressions but no types.
Read below for detail:
Variable declarations
Short variable declarations
Both are the different technique of variable declaration in Go language.
var firstName := "John" // is a variable declaration
AND
firstName := "John" // is a short variable declaration.
A short variable declaration is a shorthand for a regular variable declaration with initializer expressions but no types.
Read below for detail:
Variable declarations
Short variable declarations
answered Aug 13 '14 at 1:12
Pravin MishraPravin Mishra
5,98332944
5,98332944
9
I think your syntax is wrong for the first example. Instead of: var firstName := "John" , it should be: var firstName = "John"
– Gino
May 20 '16 at 14:10
add a comment |
9
I think your syntax is wrong for the first example. Instead of: var firstName := "John" , it should be: var firstName = "John"
– Gino
May 20 '16 at 14:10
9
9
I think your syntax is wrong for the first example. Instead of: var firstName := "John" , it should be: var firstName = "John"
– Gino
May 20 '16 at 14:10
I think your syntax is wrong for the first example. Instead of: var firstName := "John" , it should be: var firstName = "John"
– Gino
May 20 '16 at 14:10
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17891226%2fdifference-between-and-operators-in-go%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Also see this: Go Variables Visual Guide. I wrote an article about it.
– Inanc Gumus
Nov 20 '17 at 13:39
1
The semantics...
– JustDave
Jan 27 '18 at 15:27