Hacker News new | past | comments | ask | show | jobs | submit login
Go's Declaration Syntax (golang.org)
68 points by pufuwozu on July 8, 2010 | hide | past | favorite | 27 comments



As a crazy random thought, wouldn't '@' work well as an alternative to the asterisk to dereference pointers? It's available, it's ASCII, it's pronounced "at" which seems to jive with pointer semantics, and it's not used in C-like languages.

Just a very quick thought after reading Mr Pike's closing words that the use of the asterisk for pointers makes the syntax a bit clumsier than they would have wished.


That would invert the use in Pascal where

P := @X;

means assign the address of X to P. And

Y := P^;

means dereference P and assign the result to Y.

I think anyone who has written any Pascal/Delphi would prefer ^ for dereference.


Anyone who has done a lot of bit twiddling in C has other uses for ^. (I find it ironic that 4^5 has an obvious meaning outside of programming that it doesn't have in a large variety of widely used programming languages.)


I suppose they should be allocated based on frequency of use within the intended domain of the language, with the most frequent getting first choice. I guess pointer dereferencing would be more important than xor for most domains. Not that I am particularly advocating ^ for pointers, * is just as good, but I do think using @ for the opposite of its meaning in Pascal would be confusing.


C and many other languages. ^ is very nearly a universal code-XOR, at least where single-characters are concerned.


That's very funny sir, because http://ooc-lang.org/ happens to do just that.

In addition, & and @ are postfix, which pretty much settles the question.


Includes a discussion of C's declaration syntax. Some stuff in there I didn't know.


The more I use Go the more I realise how much thought has gone into every language decision and (just as importantly) how much restraint has gone into the design.

It's my first tool of choice for everything now.


The declaration syntax contradicts the commentary.

Functions do not read from left to right. The example of

  func main(argc int, argv *[]byte) int
is explained as "function main takes an int and a pointer to a slice of bytes and returns an int" but it should read "main is a function which takes an int and a pointer to a slice of bytes and returns an int" just like

  x int
means "x is an integer". Reading left to right the variable name should come first for functions as for other variables so the function syntax should be

  main func(argc int, argv *[]byte) int
But this can't be so because this appears to be the syntax that function variables use

  f func(func(int,int) int, int) int
which is explained as "Here's a declaration of a function variable (analogous to a function pointer in C)".

Also the article says "Pointers are the exception that proves the rule" because "For familiarity, Go's pointers use the * notation from C, but we could not bring ourselves to make a similar reversal for pointer types.".

Putting these two observations together I would say that Go's declaration syntax fails in its stated mission of simplicity and regularity.

EDIT: can't seem to work out how to code markup on HN, anyway I hope I've made what I'm trying to say clear enough.

UPDATE: two spaces at the start of a line seems to work


Actually, the other form works fine for declarations too:

  var main = func() { /* etc */ }
It's a minor point, though. The article is clear about how the syntax deviates from complete regularity. Your comment seems redundant.


  main func(int x, char y) int
Is an interesting idea... I don't think I've seen it in use anywhere (though I've mostly dealt with C-like). Add in the curly-braces for the definition, and you've just made functions into variables with blocks. And are they really any different anyway? The code-in-memory doesn't get duplicated when you make another instance, just the scope, so it's equivalent to a variable in a singleton.

Just pondering out loud :)


I don't think I understand your first point. The type for main would be func(int, *[]byte) int which reads left to right. The function definition syntax isn't as left to right, but it's hardly complicated or confusing.

I think your points may be valid, but I don't think they lead to your conclusion that Go's syntax fails at its stated mission.


Correct. The _type_ for main would be (is)

  func(int, *[]byte) int
but the function _declaration_ should be (by the logic of the article and the stated intentions of their design decisions)

  main func(argc int, argv *[]byte) int
and not

  func main(argc int, argv *[]byte) int
because the variable name should come first as in

  x int
and not

  int x
In this way I fee that they have, if not failed, at least deviated from their stated or implied mission. But you're right, the type reads left to right.


I hope this is the last time I see "the exception that proves the rule" in the context of programming languages. It's a wart. You know what would better prove the rule? No exceptional cases at all.


It is used correctly in the article. The meaning of the word 'prove' in that saying is anologous to 'test' or 'try'. As in 'proving grounds'.


Interesting. I never looked up the etymology of that expression. Thanks.


Yes! Absolutely. The phrase "the exception proves the rule" is a tired, worn out, clichéd contradiction in terms that must have meant something at one time I'm sure but can only be superfluous in most cases now.


I think I've heard it used reasonably, but I can't really remember the specifics. Basically, it's when the cause for the exception follows naturally from the logic behind the rule. Contrived example: If something holds for most birds, an exception for penguins might qualify.

In any case, this is just an exception that breaks the rule.


Could use a better title... it's the golang.org's blog, and it's an explanation of the syntax, which is not really implied by "Go's declaration syntax". With just the domain and title, I'd assumed this was just documentation (as sometimes gets posted on hn), not discussion, and skipped over it several times.

All in all interesting, and I definitely agree with the improvement with their "function-returning function which accepts a function as an argument" example. An interesting departure from C & languages influenced by C (ie: most).


> "function-returning function which accepts a function as an argument"

I write almost exclusively Go day to day and I've noticed I no longer shy away from function pointers like in C. It's not that the C version can't be mastered, it's just that I'd rather not occupy a significant chunk of my brain with it.

Another interesting observation is that I can now pretty easily switch between C and Go if I need to, the declaration syntax seems different enough that my brain can do the leap. Sadly less true for semicolons (Go omits most, C doesn't)


What are you doing in Go on a daily basis?


At work I'm writing internal infrastructure, at home I'm working on a CNC controller.


Indeed. C-style declarations have always been intuitionally-opaque to me (I've always had to concentrate on them to get what they're saying), but the Go example:

    f func(func(int,int) int, int) func(int, int) int
is a simple mechanical transformation away from being a Haskell declaration:

    f :: ((Int, Int) -> Int, Int) -> (Int, Int) -> Int


-1? seriously? What's objectionable about this?


You might compare the results with SPECS: http://en.wikipedia.org/wiki/Significantly_Prettier_and_Easi... -- an alternate syntax for C++. I find its productions clearer than those of Go's.


great explanation and advocacy, and also good to see Pike's name as the author, gives the language a cred boost in my eyes. maybe i'll add it to the same bucket I consider D to be in as a possible successor to C for certain niches.


We took what you're used to... and flipped it around! Did I just blow your mind??




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: