Difference between := and = operators in Go










179















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.










share|improve this question



















  • 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















179















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.










share|improve this question



















  • 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













179












179








179


26






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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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












9 Answers
9






active

oldest

votes


















144














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.






share|improve this answer























  • 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


















172














In Go, := is for declaration + assignment, whereas = is for assignment only.



For example, var foo int = 10 is the same as foo := 10.






share|improve this answer




















  • 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











  • the int is not required, var foo = 10 is the same as foo := 10

    – Gary Lyn
    Nov 21 '17 at 14:04


















35














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 =.






share|improve this answer




















  • 2





    Thanks for mentioning all the use cases.

    – AnirbanDebnath
    Jan 28 '18 at 4:43


















18














:= 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).






share|improve this answer
































    9














    := 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)





    share|improve this answer






























      8














      The := means declare and assign while the = means to simply assign.






      share|improve this answer






























        6














        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.






        share|improve this answer






























          2














          “:=” 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






          share|improve this answer






























            1














            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






            share|improve this answer


















            • 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











            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
            );



            );













            draft saved

            draft discarded


















            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









            144














            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.






            share|improve this answer























            • 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















            144














            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.






            share|improve this answer























            • 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













            144












            144








            144







            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.






            share|improve this answer













            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            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

















            • 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













            172














            In Go, := is for declaration + assignment, whereas = is for assignment only.



            For example, var foo int = 10 is the same as foo := 10.






            share|improve this answer




















            • 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











            • the int is not required, var foo = 10 is the same as foo := 10

              – Gary Lyn
              Nov 21 '17 at 14:04















            172














            In Go, := is for declaration + assignment, whereas = is for assignment only.



            For example, var foo int = 10 is the same as foo := 10.






            share|improve this answer




















            • 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











            • the int is not required, var foo = 10 is the same as foo := 10

              – Gary Lyn
              Nov 21 '17 at 14:04













            172












            172








            172







            In Go, := is for declaration + assignment, whereas = is for assignment only.



            For example, var foo int = 10 is the same as foo := 10.






            share|improve this answer















            In Go, := is for declaration + assignment, whereas = is for assignment only.



            For example, var foo int = 10 is the same as foo := 10.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            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











            • the int is not required, var foo = 10 is the same as foo := 10

              – Gary Lyn
              Nov 21 '17 at 14:04












            • 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











            • the int is not required, var foo = 10 is the same as foo := 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











            35














            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 =.






            share|improve this answer




















            • 2





              Thanks for mentioning all the use cases.

              – AnirbanDebnath
              Jan 28 '18 at 4:43















            35














            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 =.






            share|improve this answer




















            • 2





              Thanks for mentioning all the use cases.

              – AnirbanDebnath
              Jan 28 '18 at 4:43













            35












            35








            35







            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 =.






            share|improve this answer















            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 =.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            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












            • 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











            18














            := 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).






            share|improve this answer





























              18














              := 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).






              share|improve this answer



























                18












                18








                18







                := 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).






                share|improve this answer















                := 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).







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jul 27 '13 at 6:19

























                answered Jul 27 '13 at 6:03









                ShuklaSannidhyaShuklaSannidhya

                3,86842443




                3,86842443





















                    9














                    := 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)





                    share|improve this answer



























                      9














                      := 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)





                      share|improve this answer

























                        9












                        9








                        9







                        := 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)





                        share|improve this answer













                        := 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)






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Jul 26 '13 at 21:28









                        GustavGustav

                        2,29311725




                        2,29311725





















                            8














                            The := means declare and assign while the = means to simply assign.






                            share|improve this answer



























                              8














                              The := means declare and assign while the = means to simply assign.






                              share|improve this answer

























                                8












                                8








                                8







                                The := means declare and assign while the = means to simply assign.






                                share|improve this answer













                                The := means declare and assign while the = means to simply assign.







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Jul 26 '13 at 21:25









                                Ralph CaraveoRalph Caraveo

                                5,89753045




                                5,89753045





















                                    6














                                    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.






                                    share|improve this answer



























                                      6














                                      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.






                                      share|improve this answer

























                                        6












                                        6








                                        6







                                        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.






                                        share|improve this answer













                                        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.







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Aug 23 '13 at 8:05









                                        subhash kumar singhsubhash kumar singh

                                        1,43842539




                                        1,43842539





















                                            2














                                            “:=” 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






                                            share|improve this answer



























                                              2














                                              “:=” 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






                                              share|improve this answer

























                                                2












                                                2








                                                2







                                                “:=” 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






                                                share|improve this answer













                                                “:=” 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







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Mar 16 '17 at 4:22









                                                Nisal EduNisal Edu

                                                2,89021423




                                                2,89021423





















                                                    1














                                                    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






                                                    share|improve this answer


















                                                    • 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
















                                                    1














                                                    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






                                                    share|improve this answer


















                                                    • 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














                                                    1












                                                    1








                                                    1







                                                    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






                                                    share|improve this answer













                                                    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







                                                    share|improve this answer












                                                    share|improve this answer



                                                    share|improve this answer










                                                    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













                                                    • 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


















                                                    draft saved

                                                    draft discarded
















































                                                    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.




                                                    draft saved


                                                    draft discarded














                                                    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





















































                                                    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







                                                    這個網誌中的熱門文章

                                                    How to read a connectionString WITH PROVIDER in .NET Core?

                                                    Node.js Script on GitHub Pages or Amazon S3

                                                    Museum of Modern and Contemporary Art of Trento and Rovereto