Go syntax choices

I am new to Go and I am curious as to why Go has deviated from typical syntax of declaring variable type before variable name, or specifying the return type after the function name. What’s the benefit of introducing keyword ‘var’? Any background on why the language designers would prefer ‘var my_var int’ over ‘int my_var’?

This was the biggest issue for me, when I started learning Go a year ago. My belief was that the creators of Go were evil and just wanted to piss everyone off. Now I have gotten used to it, and I don’t complain anymore. But it is annoying when you program in several different languages, and one of them is different in that way. Sorry, I don’t have an answer for you though…

:smile: Thanks @Hultan! Given both Rob Pike and Ken Thompson come from C background and seeing how this language is built to simplify memory management of C, I got curious about the choice. Another thing that I find interesting is multiple return values from a function. Is it really a function if it returns two values?

I own view is that the creators of Go saw the way it was done in C and decided that it was wrong and needed to change.

Here is how you define an array of 4 function pointers in C

int (*p[4]) (int x, int y);

and here is what it looks like in go

var p [4]func(x int, y int) int

I think the Go is much easier to read… from left to right it says define a variable var called p which is an array of size 4 [4] each element of which is a function pointer func which takes a parameter called x which is an int and a parameter called y also an int and the function returns an int.

Whereas working out what the C style declaration means is a mind bender which involves starting in the inside at the p and working your way out. Here is an article about how to read C style type definitions.

All that flows from reversed declarations!

The authoritative answers:


Go: Frequently Asked Questions (FAQ)

Why are declarations backwards?


The Go Blog: Go’s Declaration Syntax

Heh. Similarly, one of my gripes with Go syntax is the ‘type’ keyword. I don’t see why they couldn’t just eliminate it, and let us just prefix the only two types in Go with their exact name: struct and interface, ie:

type Blah struct { }
type Bleh interface { }

Should just be:

struct Blah { }
interface Blah { }

…and then they could reserve ‘type’ for just actually creating type aliases, ie:

type SomeTypeAlias int;

Removing type for struct and interface makes the code more readable, makes more cognitive sense when typing, and is more brief.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.