telo_tade
(telo tade)
March 14, 2022, 8:09am
1
Hello all,
body, err := record.MarshalToString()
if err != nil {
return err
}
Versus:
var err error
var body string
if body, err = record.MarshalToString(); err != nil {
return err
}
I cannot tell which one I should be using, as I keep seeing both versions in the same repo.
mje
(Jeff Emanuel)
March 14, 2022, 3:30pm
2
The second doesn’t really provide any benefit. Doing the call and test together in the if is useful to limit the scope of the err
variable. I follow the first pattern in this situation.
1 Like
NobbZ
(Norbert Melzer)
March 14, 2022, 4:22pm
3
Personally I perfer the second approach, if and only if, I can use :=
and keep the scope of variables extremely limited.
4 Likes
amnon
(amnon bc)
March 15, 2022, 9:49am
4
first is far more readable.
Cornos
(Oussama)
March 21, 2022, 7:43am
5
If you don’t know follow this simple rule:
If you are initializing an identifier to its zero value use var
.
If you want to initialize a value to an identifier other than the zero value use :=
system
(system)
Closed
June 19, 2022, 7:43am
6
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.