Why declaring multiple variable at once for same type feature exists in Golang?

616 views
Skip to first unread message

Yasser Sinjab

unread,
Aug 18, 2022, 8:52:56 AM8/18/22
to golang-nuts
Hi,

I'm just wondering why this feature is in Golang: such as 

var x, y, z string

Thanks,
Yasser

Brian Candler

unread,
Aug 18, 2022, 12:35:21 PM8/18/22
to golang-nuts
What exactly is it about that expression that you don't think seems right?

It's a shortcut for:

var x string
var y string
var z string

I think that saving typing is a good feature.

Or do you object to the feature of being able to declare variables at all?

Brian Candler

unread,
Aug 18, 2022, 12:38:54 PM8/18/22
to golang-nuts
There's also a lot of good background about the language design choices in the FAQ:

Go derives much of its syntax from C, and the FAQ describes some of the differences from C.  But note that even C allows you to do the same:

int a, b, c;
char *x, *y, *z;

Yasser Sinjab

unread,
Aug 18, 2022, 10:25:22 PM8/18/22
to golang-nuts
Thanks, I don't object it personally, but I had a debate about "grouping variables is considered a clean code". 

Let's say you have the following code:

var x, y, z string
var i int

And a new change came as:

var j int
var x, y, z string
var i int

For me this is not clean. Group them is cleaner like : 

var i, j int


That's of course with the exception that variables should be as close to the logic it is initialized for.

K. Alex Mills

unread,
Aug 18, 2022, 10:29:36 PM8/18/22
to Yasser Sinjab, golang-nuts
Yes. Declaring i and j on the same line is certainly cleaner. We should all do it like that.

Am I missing something?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/ec5c344a-3a67-49d1-9da7-ec3633cfb903n%40googlegroups.com.

Kurtis Rader

unread,
Aug 18, 2022, 10:48:11 PM8/18/22
to Yasser Sinjab, golang-nuts
On Thu, Aug 18, 2022 at 7:25 PM Yasser Sinjab <ysi...@gmail.com> wrote:
Thanks, I don't object it personally, but I had a debate about "grouping variables is considered a clean code". 

I get the sense you were having a discussion with a coworker (or someone you were collaborating with on an open-source project) who objected to grouping var declarations with the same type and you are looking for validation of your viewpoint that grouping the declarations is preferable. Questions of code style are always fraught. That is, likely to lead to arguments with no obvious, definitive, reason to prefer one argument over all the others since there is some subjectiveness to preferring a particular style rule. Having said that I don't see any reason to prefer separate statements for each declaration of a var of the same type. My preference is, within reason, whatever minimizes the number of vertical lines to express the same intent.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/ec5c344a-3a67-49d1-9da7-ec3633cfb903n%40googlegroups.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

SAIFI

unread,
Aug 19, 2022, 2:12:28 PM8/19/22
to golan...@googlegroups.com
Because it's the greatest programming language, ever !

Saifi.
Reply all
Reply to author
Forward
0 new messages