When to use var or := in Go? [duplicate]












-3















This question already has an answer here:




  • Why there are two ways of declaring variables in Go, what's the difference and which to use?

    1 answer



  • var vs := in Go

    3 answers




Is there any difference between the following two examples?



type Example struct {}

func main() {
e := Example{}
}


vs.



type Example struct {}

func main() {
var e Example
}


Is there a preferable one?



Thanks!










share|improve this question















marked as duplicate by peterSO, Volker, Flimzy, icza go
Users with the  go badge can single-handedly close go questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 9:53


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    In your example, both cases are equally valid as both result in the default value of the Example type. I would prefer using the := syntax since most cases you will not initialize a variable with the default value.
    – manemarron
    Nov 21 '18 at 2:07










  • They are identical. It's a style thing. I use var if I'm not going to initialize the variable and := if I am going to initialize it (i.e., to something other than the zero value).
    – Andy Schweig
    Nov 21 '18 at 2:36
















-3















This question already has an answer here:




  • Why there are two ways of declaring variables in Go, what's the difference and which to use?

    1 answer



  • var vs := in Go

    3 answers




Is there any difference between the following two examples?



type Example struct {}

func main() {
e := Example{}
}


vs.



type Example struct {}

func main() {
var e Example
}


Is there a preferable one?



Thanks!










share|improve this question















marked as duplicate by peterSO, Volker, Flimzy, icza go
Users with the  go badge can single-handedly close go questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 9:53


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    In your example, both cases are equally valid as both result in the default value of the Example type. I would prefer using the := syntax since most cases you will not initialize a variable with the default value.
    – manemarron
    Nov 21 '18 at 2:07










  • They are identical. It's a style thing. I use var if I'm not going to initialize the variable and := if I am going to initialize it (i.e., to something other than the zero value).
    – Andy Schweig
    Nov 21 '18 at 2:36














-3












-3








-3








This question already has an answer here:




  • Why there are two ways of declaring variables in Go, what's the difference and which to use?

    1 answer



  • var vs := in Go

    3 answers




Is there any difference between the following two examples?



type Example struct {}

func main() {
e := Example{}
}


vs.



type Example struct {}

func main() {
var e Example
}


Is there a preferable one?



Thanks!










share|improve this question
















This question already has an answer here:




  • Why there are two ways of declaring variables in Go, what's the difference and which to use?

    1 answer



  • var vs := in Go

    3 answers




Is there any difference between the following two examples?



type Example struct {}

func main() {
e := Example{}
}


vs.



type Example struct {}

func main() {
var e Example
}


Is there a preferable one?



Thanks!





This question already has an answer here:




  • Why there are two ways of declaring variables in Go, what's the difference and which to use?

    1 answer



  • var vs := in Go

    3 answers








go






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 7:16









Eddy Hernandez

1,5311120




1,5311120










asked Nov 21 '18 at 1:59









Evanusso

457




457




marked as duplicate by peterSO, Volker, Flimzy, icza go
Users with the  go badge can single-handedly close go questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 9:53


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by peterSO, Volker, Flimzy, icza go
Users with the  go badge can single-handedly close go questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 9:53


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    In your example, both cases are equally valid as both result in the default value of the Example type. I would prefer using the := syntax since most cases you will not initialize a variable with the default value.
    – manemarron
    Nov 21 '18 at 2:07










  • They are identical. It's a style thing. I use var if I'm not going to initialize the variable and := if I am going to initialize it (i.e., to something other than the zero value).
    – Andy Schweig
    Nov 21 '18 at 2:36














  • 1




    In your example, both cases are equally valid as both result in the default value of the Example type. I would prefer using the := syntax since most cases you will not initialize a variable with the default value.
    – manemarron
    Nov 21 '18 at 2:07










  • They are identical. It's a style thing. I use var if I'm not going to initialize the variable and := if I am going to initialize it (i.e., to something other than the zero value).
    – Andy Schweig
    Nov 21 '18 at 2:36








1




1




In your example, both cases are equally valid as both result in the default value of the Example type. I would prefer using the := syntax since most cases you will not initialize a variable with the default value.
– manemarron
Nov 21 '18 at 2:07




In your example, both cases are equally valid as both result in the default value of the Example type. I would prefer using the := syntax since most cases you will not initialize a variable with the default value.
– manemarron
Nov 21 '18 at 2:07












They are identical. It's a style thing. I use var if I'm not going to initialize the variable and := if I am going to initialize it (i.e., to something other than the zero value).
– Andy Schweig
Nov 21 '18 at 2:36




They are identical. It's a style thing. I use var if I'm not going to initialize the variable and := if I am going to initialize it (i.e., to something other than the zero value).
– Andy Schweig
Nov 21 '18 at 2:36












3 Answers
3






active

oldest

votes


















1














Might worth noting:



Use := when you need to create a variable with a defined value on it.



number := 12
obj := SomeStruct{name: "user"}
slice := string{"a", "b", "c"}




Use var keyword when you need to define a variable without any initialisation, so the zero value will be used on it.



var a int // the default value will be the zero value of int, which is 0
var mut sync.Mutex
var result map[string]interface{}


Also there is another advantage of using var keyword, like we can create multiple variable with one type in one line.



var result1, result2, result3, result4 map[string]interface{}





share|improve this answer































    1














    var is mostly used to define variables in a shared scope. For example,



    var something string

    func init() {
    something = "Hello world!"
    }

    func main() {
    fmt.Println(something)
    }





    share|improve this answer





















    • @ThunderCat You could use it also outside of a for or an if statement, so it is not necessarily the package scope
      – manemarron
      Nov 21 '18 at 2:52





















    1














    Have no different in here, just a habit. I often use var e Example.
    := can not using outside function while var can do it.






    share|improve this answer






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Might worth noting:



      Use := when you need to create a variable with a defined value on it.



      number := 12
      obj := SomeStruct{name: "user"}
      slice := string{"a", "b", "c"}




      Use var keyword when you need to define a variable without any initialisation, so the zero value will be used on it.



      var a int // the default value will be the zero value of int, which is 0
      var mut sync.Mutex
      var result map[string]interface{}


      Also there is another advantage of using var keyword, like we can create multiple variable with one type in one line.



      var result1, result2, result3, result4 map[string]interface{}





      share|improve this answer




























        1














        Might worth noting:



        Use := when you need to create a variable with a defined value on it.



        number := 12
        obj := SomeStruct{name: "user"}
        slice := string{"a", "b", "c"}




        Use var keyword when you need to define a variable without any initialisation, so the zero value will be used on it.



        var a int // the default value will be the zero value of int, which is 0
        var mut sync.Mutex
        var result map[string]interface{}


        Also there is another advantage of using var keyword, like we can create multiple variable with one type in one line.



        var result1, result2, result3, result4 map[string]interface{}





        share|improve this answer


























          1












          1








          1






          Might worth noting:



          Use := when you need to create a variable with a defined value on it.



          number := 12
          obj := SomeStruct{name: "user"}
          slice := string{"a", "b", "c"}




          Use var keyword when you need to define a variable without any initialisation, so the zero value will be used on it.



          var a int // the default value will be the zero value of int, which is 0
          var mut sync.Mutex
          var result map[string]interface{}


          Also there is another advantage of using var keyword, like we can create multiple variable with one type in one line.



          var result1, result2, result3, result4 map[string]interface{}





          share|improve this answer














          Might worth noting:



          Use := when you need to create a variable with a defined value on it.



          number := 12
          obj := SomeStruct{name: "user"}
          slice := string{"a", "b", "c"}




          Use var keyword when you need to define a variable without any initialisation, so the zero value will be used on it.



          var a int // the default value will be the zero value of int, which is 0
          var mut sync.Mutex
          var result map[string]interface{}


          Also there is another advantage of using var keyword, like we can create multiple variable with one type in one line.



          var result1, result2, result3, result4 map[string]interface{}






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 0:48

























          answered Nov 21 '18 at 2:39









          xpare

          4,6372248




          4,6372248

























              1














              var is mostly used to define variables in a shared scope. For example,



              var something string

              func init() {
              something = "Hello world!"
              }

              func main() {
              fmt.Println(something)
              }





              share|improve this answer





















              • @ThunderCat You could use it also outside of a for or an if statement, so it is not necessarily the package scope
                – manemarron
                Nov 21 '18 at 2:52


















              1














              var is mostly used to define variables in a shared scope. For example,



              var something string

              func init() {
              something = "Hello world!"
              }

              func main() {
              fmt.Println(something)
              }





              share|improve this answer





















              • @ThunderCat You could use it also outside of a for or an if statement, so it is not necessarily the package scope
                – manemarron
                Nov 21 '18 at 2:52
















              1












              1








              1






              var is mostly used to define variables in a shared scope. For example,



              var something string

              func init() {
              something = "Hello world!"
              }

              func main() {
              fmt.Println(something)
              }





              share|improve this answer












              var is mostly used to define variables in a shared scope. For example,



              var something string

              func init() {
              something = "Hello world!"
              }

              func main() {
              fmt.Println(something)
              }






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 21 '18 at 2:03









              manemarron

              888




              888












              • @ThunderCat You could use it also outside of a for or an if statement, so it is not necessarily the package scope
                – manemarron
                Nov 21 '18 at 2:52




















              • @ThunderCat You could use it also outside of a for or an if statement, so it is not necessarily the package scope
                – manemarron
                Nov 21 '18 at 2:52


















              @ThunderCat You could use it also outside of a for or an if statement, so it is not necessarily the package scope
              – manemarron
              Nov 21 '18 at 2:52






              @ThunderCat You could use it also outside of a for or an if statement, so it is not necessarily the package scope
              – manemarron
              Nov 21 '18 at 2:52













              1














              Have no different in here, just a habit. I often use var e Example.
              := can not using outside function while var can do it.






              share|improve this answer




























                1














                Have no different in here, just a habit. I often use var e Example.
                := can not using outside function while var can do it.






                share|improve this answer


























                  1












                  1








                  1






                  Have no different in here, just a habit. I often use var e Example.
                  := can not using outside function while var can do it.






                  share|improve this answer














                  Have no different in here, just a habit. I often use var e Example.
                  := can not using outside function while var can do it.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 21 '18 at 2:18

























                  answered Nov 21 '18 at 2:04









                  KibGzr

                  1,466610




                  1,466610















                      Popular posts from this blog

                      Create new schema in PostgreSQL using DBeaver

                      Deepest pit of an array with Javascript: test on Codility

                      Costa Masnaga