News for hackers. Stuff that matters.
Re:Learn Go in five minutes by cute_boi on Tuesday January 05, 2021 @11:56AM (#25647509)
I like C but I would prefer Rust over Go. Go may be simple but I really hate using conventions like init(), comments working like syntax, captial letter means published etc.
Re:Learn Go in five minutes by corytheboyd on Tuesday January 05, 2021 @12:19PM (#25647872)
Doesn’t Rust have “comments working like syntax” in the form of #[derive] etc.?

I’ve only read through the rust book without using it for anything (yet) and have done no work on golang, so I’m legitimately curious to hear your input, not challenging you on semantics :p

Edit: Rust does not use # for comments. Sorry I just had that mixed up in my head, but thanks for the answers!

Re:Learn Go in five minutes by cesarb on Tuesday January 05, 2021 @12:31PM (#25648077)
These #[...] and #![...] are not comments; they're attributes (somewhat similar to Java's @Something annotations). Comments in Rust start with // or /<star> like in C or C++. There's only the special case that "doc comments" (starting with /// or /<star><star> or //! or /<star>!) are automatically converted into #[doc="..."] or #![doc="..."] attributes.

The difference is that attributes are always meaningful to the compiler (and can only be used in specific places); other than doc comments (which turn into attributes), comments never are meaningful to the compiler.

(edit: HN's comment markup eats the * character, I replaced them with <star> above)

Re:Learn Go in five minutes by brundolf on Tuesday January 05, 2021 @12:26PM (#25647993)
#[...] is an attribute, more like @foo annotations in Java, not a comment. Comments do not use # in Rust
Re:Learn Go in five minutes by corytheboyd on Tuesday January 05, 2021 @12:32PM (#25648091)
> Comments do not use # in Rust

Oof what a dumb mistake, sorry about that!

Re:Learn Go in five minutes by felipellrocha on Tuesday January 05, 2021 @02:35PM (#25649922)
It's ok. Other people probably had the same question!
Re:Learn Go in five minutes by Thaxll on Tuesday January 05, 2021 @12:12PM (#25647755)
init() is not a convention and it's not used that much. For comments I don't understand what you're saying. And for caps I think it's better than public / private keyword.
Re:Learn Go in five minutes by fwip on Tuesday January 05, 2021 @12:22PM (#25647911)
Certain comments have a special meaning to some tooling. In this sense, it's not very different from Javadocs.

For some examples: A comment that appears right before a type or function definition is exported as documentation by the "go doc" tool.

Another one is build constraints, which appear as a comment at the top of the file, like: `// +build linux` to say "only include this file if the build target is linux."

Other tools use their own comment format to recognize comments meant for them.

Re:Learn Go in five minutes by steve_adams_86 on Tuesday January 05, 2021 @12:10PM (#25647733)
Do Go comments work like syntax? Do you mean that they need to be formatted a certain way like they’re language syntax?
Re:Learn Go in five minutes by grey-area on Tuesday January 05, 2021 @01:27PM (#25648959)
The Go authors have (ab)used comments to extend the language in experimental ways for code generation, build pragmas and recently file embedding.

They’ve done this to avoid breaking the language stability guarantee, not sure if there were other reasons. I think it’s a mistake personally to create this sort of metalanguage in comments (now you have two problems!) but it’s easy to avoid using this most of the time as the uses are pretty esoteric.

Re:Learn Go in five minutes by skrtskrt on Tuesday January 05, 2021 @02:35PM (#25649923)
File embedding is a common enough use case (HTML templates, config files) that it really makes me cringe that it's being done with comments.
Re:Learn Go in five minutes by grey-area on Tuesday January 05, 2021 @03:49PM (#25650820)
I just keep my templates and config in separate files.

There are good reasons to keep them separate anyway (allows config without recompile for example) and for the apps I work on distribution isn’t a problem. So I haven’t really had to use comments as code in 7-8 years of go. Perhaps it’ll become more common with the embed stuff. But then I don’t use struct tags either so I guess I’d prefer Go to be even simpler.

Re:Learn Go in five minutes by _ph_ on Tuesday January 05, 2021 @02:38PM (#25649952)
No, Go comments are just comments. They have been used though to add meta-information to special tools. So unless you use those tools and require to add the specific meta-information, you can treat comments as plain comments.

Hiding certain meta-information, like exporting a function with a C-compatible signature when building a shared library is not a great way of doing things, but on the other side, it is a special usage which isn't part of the language itself, so not part of its syntax.