In Go, what is the difference between 'var' and ':=' when declaring variables?

  • Declares a constant
  • Declares a variable and assigns a value without specifying its type
  • Declares a variable with a shorthand syntax
  • Declares a variable with an explicit type
In Go, var is used to declare variables with an explicit type, whereas := is a shorthand syntax that both declares a variable and assigns a value to it without needing to specify the type explicitly. This shorthand syntax is only available within functions, where type inference can be used.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *