When to use var or := in Go? [duplicate]
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!
go
marked as duplicate by peterSO, Volker, Flimzy, icza
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.
add a comment |
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!
go
marked as duplicate by peterSO, Volker, Flimzy, icza
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 theExample
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 usevar
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
add a comment |
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!
go
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
go
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
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
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 theExample
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 usevar
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
add a comment |
1
In your example, both cases are equally valid as both result in the default value of theExample
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 usevar
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
add a comment |
3 Answers
3
active
oldest
votes
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{}
add a comment |
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)
}
@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
add a comment |
Have no different in here, just a habit. I often use var e Example
.:=
can not using outside function while var
can do it.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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{}
add a comment |
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{}
add a comment |
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{}
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{}
edited Nov 22 '18 at 0:48
answered Nov 21 '18 at 2:39
xpare
4,6372248
4,6372248
add a comment |
add a comment |
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)
}
@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
add a comment |
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)
}
@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
add a comment |
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)
}
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)
}
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
add a comment |
@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
add a comment |
Have no different in here, just a habit. I often use var e Example
.:=
can not using outside function while var
can do it.
add a comment |
Have no different in here, just a habit. I often use var e Example
.:=
can not using outside function while var
can do it.
add a comment |
Have no different in here, just a habit. I often use var e Example
.:=
can not using outside function while var
can do it.
Have no different in here, just a habit. I often use var e Example
.:=
can not using outside function while var
can do it.
edited Nov 21 '18 at 2:18
answered Nov 21 '18 at 2:04
KibGzr
1,466610
1,466610
add a comment |
add a comment |
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