TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Go's Declaration Syntax

68 点作者 pufuwozu将近 15 年前

8 条评论

igravious将近 15 年前
The declaration syntax contradicts the commentary.<p>Functions do not read from left to right. The example of<p><pre><code> func main(argc int, argv *[]byte) int </code></pre> 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<p><pre><code> x int </code></pre> 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<p><pre><code> main func(argc int, argv *[]byte) int </code></pre> But this can't be so because this appears to be the syntax that function variables use<p><pre><code> f func(func(int,int) int, int) int </code></pre> which is explained as "Here's a declaration of a function variable (analogous to a function pointer in C)".<p>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.".<p>Putting these two observations together I would say that Go's declaration syntax fails in its stated mission of simplicity and regularity.<p>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.<p>UPDATE: two spaces at the start of a line seems to work
评论 #1499070 未加载
评论 #1498030 未加载
评论 #1497719 未加载
评论 #1497529 未加载
unwind将近 15 年前
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.<p>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.
评论 #1496775 未加载
评论 #1526799 未加载
enneff将近 15 年前
Includes a discussion of C's declaration syntax. Some stuff in there I didn't know.
sambeau将近 15 年前
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.<p>It's my first tool of choice for everything now.
mkramlich将近 15 年前
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.
Groxx将近 15 年前
Could use a better title... it's the golang.org's <i>blog</i>, and it's an explanation <i>of</i> 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.<p>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 &#38; languages influenced by C (ie: most).
评论 #1496680 未加载
评论 #1496549 未加载
评论 #1497408 未加载
jeberle将近 15 年前
You might compare the results with SPECS: <a href="http://en.wikipedia.org/wiki/Significantly_Prettier_and_Easier_C%2B%2B_Syntax" rel="nofollow">http://en.wikipedia.org/wiki/Significantly_Prettier_and_Easi...</a> -- an alternate syntax for C++. I find its productions clearer than those of Go's.
heatdeath将近 15 年前
We took what you're used to... and flipped it around! Did I just blow your mind??