News for hackers. Stuff that matters.
Re:Just tell me how to use Go Modules by nstart on Monday December 10, 2018 @11:53PM (#18653359)
Just going to share a sentiment I've felt while diving into Golang over several months. The canonical way to do things feels like it's all over the place in several areas of Golang. Not in the fundamental "this is how you should indent" bit but in the "this is how you should unpack dynamic json content" sense.

I really wish golang had more canonical content. But at this point, I feel like I'm working against the grain. Golang doesn't feel like a language that wants to prescribe a lot more than fundamentals. How you structure your code beyond the src/git/path/your-code/ is completely up to you and every repo you visit could be different. It's not my thing personally, and I'm leaving this comment just in case any "want to try golang" folk stop by. This is something you should be aware of going in. Don't bang your head when you can't find a clear way of doing something that probably seemed simple in other languages. Embrace the new concept and see if you can get used to it.

That said, if anyone ever breaks into the eco system with an es lint style tool that allows experts to share their best practices via plugins, that would be awesome. If that exists already, please do let me know. I'd love to give it a go.

Re:Just tell me how to use Go Modules by weberc2 on Tuesday December 11, 2018 @12:25AM (#18653431)
> How you structure your code beyond the src/git/path/your-code/ is completely up to you and every repo you visit could be different.

This is an unusual criticism. Typically Go is faulted for being too opinionated, and it’s projects too predictable. In fact, this is why I took to the language—no need to see past every maintainer’s personal flourishes just to build the code or run the tests. No ceremoniously nested directory structure. No figuring out how to point the build tool to the source files or how to convince the build tool to output or run a test target. Everything is right there, rather plainly and consistently across all projects. This does mean I have less room for creativity which is an okay tradeoff for me because I have just enough creativity to spend on the application and its architecture and the rest should just work, but I recognize that some may disagree and that’s okay too.

Re:Just tell me how to use Go Modules by Felz on Tuesday December 11, 2018 @01:15AM (#18653544)
This sounds like tooling and formatting consistency versus abstraction consistency. Go isn't great for building powerful abstractions, so at a higher level implementations will trend towards inconsistency, however consistent the low level syntax.
Re:Just tell me how to use Go Modules by weberc2 on Tuesday December 11, 2018 @10:32AM (#18655924)
A few thoughts:

1. I don't think powerful abstractions are often that useful. I use Python in my day job, and we mostly don't use many high level abstractions. We never use monads, for example, and while we do use async futures, these are strictly worse than Go's concurrency features.

2. Pretty sure abstraction power drives _less_ consistency, not more. Effectually, it means there are more ways to do the same thing (and more ways to screw it up and make a mess), and consistency depends on organization discipline and less on the rails of the language. I don't mean to say abstraction is bad--only that consistency is one of the costs, not an advantage.

I'm interested to hear your thoughts.

Re:Just tell me how to use Go Modules by zeugmasyllepsis on Tuesday December 11, 2018 @12:09PM (#18656756)
> Pretty sure abstraction power drives _less_ consistency, not more.

It depends, I think. Abstractions allow users to specify part of a program's behavior independently from others. This is not intended to start a flamewar or pick on Go, but for better or worse Go does not support user defined generic functions. One consequence is that certain kinds of methods are difficult to write in a generic, performant, and type-safe way. If users want performance and type safety, they need multiple implementations of those methods for each desired type. This leads to multiple copies of very similar code (which can evolve independently, lowering consistency) or the use of on of the community-driven code generators (which all have their own differences, again lowering consistency).

Adding generics could largely make all of this inconsistency go away, but as you point out would enable a whole slew of other programming patterns more ways to make a mess.

I prefer to think of abstraction techniques as shifting consistency. Ideally, they provide an obvious way to factor out one piece of a program's behavior, allowing developers to focus on the rest of the behavior independently.

Re:Just tell me how to use Go Modules by weberc2 on Tuesday December 11, 2018 @02:36PM (#18658136)
I largely agree, but I think people generally underestimate the abstractive power of (typesafe) Go and overestimate the cost of its abstractions.

Generally Go's abstractions are based on dynamic dispatch (virtual function calls) which are quite cheap and are the basis for most other languages' abstractions. There are languages like Rust and C++ which offer zero-cost abstractions and generally beat Go in performance and abstraction power, but lose in other ways. If you're comparing Go to Java, C#, Haskell, OCaml, Python, JS, and Ruby, Go performs on par or better.

As far as abstractive power, Go does quite well, although there is still some _boilerplate_ (for loops and closures); however, this is generally not where your bugs come from (certainly not your troublesome bugs) and boilerplate is consistent by definition. I think the biggest reason Go's lack of generics irks people is because they want _terse_ code, because terseness is the most obvious (but least useful) kind of DRYness. The next biggest reason is more legitimate--getting a lot of typesafe abstractive power out of Go requires a different bag of tricks (and a different way of thinking about abstraction) than languages with generics.

Re:Just tell me how to use Go Modules by bpicolo on Tuesday December 11, 2018 @01:06PM (#18657277)
> never use monads

I think "powerful abstractions" fall into other categories, e.g. ORMs. While gORM kinda-sorta works in the context of golang limitations, it's missing a lot of stuff that doesn't currently fit into the go model.

Re:Just tell me how to use Go Modules by weberc2 on Tuesday December 11, 2018 @02:03PM (#18657845)
Can you elaborate? I can't think of anything in Go that would preclude ORMs. I'm not familiar with gORM, however.
Re:Just tell me how to use Go Modules by gcb0 on Tuesday December 11, 2018 @02:06AM (#18653655)
in other words, it's consistent if you only look at it for a few minutes. use it for anything and it become an inconsistent mess.
Re:Just tell me how to use Go Modules by kerng on Tuesday December 11, 2018 @10:01AM (#18655700)
Been trying it out and agree. I like it a lot for simple, stand alone tools. For more complex ones it appears strangely ill-suited and "messy".
Re:Just tell me how to use Go Modules by jonathanstrange on Tuesday December 11, 2018 @04:27AM (#18654122)
As if powerful abstractions lead to more consistency...
Re:Just tell me how to use Go Modules by the_clarence on Tuesday December 11, 2018 @12:22PM (#18656871)
You mean 50 layers of abstraction?
Re:Just tell me how to use Go Modules by kodablah on Tuesday December 11, 2018 @05:47AM (#18654440)
>> Not in the fundamental "this is how you should indent" bit but in the "this is how you should unpack dynamic json content" sense

> Typically Go is faulted for being too opinionated, and it’s projects too predictable

The parent addressed this. You are confusing inflexibility of language with inconsistency of ecosystem. Devs can want both more flexibility in the language, syntax, and type system and more commonly accepted project-level structures/approaches at the same time. I definitely don't see projects being too predictable at a general level as each lib has its own way of doing many things.

Re:Just tell me how to use Go Modules by calgoo on Tuesday December 11, 2018 @12:31PM (#18656961)
I agree, I love it even more for making small tools where i can just skip the whole folder structure and just have a .go file in a directory and compile it to a bin without all the fuzz of build tools, special folder structures etc. Of course for bigger, shared projects i know these tools help, but sometimes when you just want to make something small, or test something, i find these tools just get in my way.
Re:Just tell me how to use Go Modules by jjtheblunt on Tuesday December 11, 2018 @05:29PM (#18659598)
go build -o whatever.exe whatever.go works just fine.
Re:Just tell me how to use Go Modules by fouc on Tuesday December 11, 2018 @03:20AM (#18653905)
When I hear "opinionated" I think of Ruby on Rails. Would be nice if Go had more opinions on structure and conventions perhaps.
Re:Just tell me how to use Go Modules by Someone on Tuesday December 11, 2018 @03:32AM (#18653951)
Frameworks provide structure; Ruby on Rails is a framework, Go is not.
Re:Just tell me how to use Go Modules by fouc on Tuesday December 11, 2018 @06:02AM (#18654513)
Go is already halfway there ;)
Re:Just tell me how to use Go Modules by randomdata on Tuesday December 11, 2018 @09:07AM (#18655348)
Even Rails only manages to be opinionated for a small set of aspects about building CRUD web applications. If you can stay within those boundaries, it is useful. But as soon as your application needs to deviate from that, it seems that every project goes off and does its own thing again.
Re:Just tell me how to use Go Modules by anonfunction on Tuesday December 11, 2018 @02:22AM (#18653703)
If you frequent reddit/r/golang you'll see this question pops up quite frequently and the comments have many differing suggestions.
Re:Just tell me how to use Go Modules by andy_ppp on Tuesday December 11, 2018 @02:42AM (#18653770)
Great! What are they?
Re:Just tell me how to use Go Modules by skolsuper on Tuesday December 11, 2018 @06:49AM (#18654691)
Comments are snippets of text, typically short, written by consumers of content in order to share their opinion of or reaction to that content with other consumers.
Re:Just tell me how to use Go Modules by jzoch on Tuesday December 11, 2018 @12:07AM (#18653389)
To agree with you with a slight rephrase: what makes idiomatic go code is very murky lately. With new changes in the horizon (Go 2 proposals around error checking and generics) the current solutions just dont feel idiomatic. Before these proposals started coming in, people staunchly (and many still do) defended error checking and lack of generics. Now, it feels a bit weak.

Why am I writing all this ceremony (comparatively little compared to other languages, but ceremony nonetheless) when the new, better way is coming?

Additionally, I agree that the idiomatic design patterns of go aren't well defined beyond "use channels, share memory by communicating, handle errors, and dont ever use functional precepts". A lot of go feels really good which makes the not as good parts really stand out.

Personally, I'd like to see small QoL improvements (beyond generics and error handling) like named arguments, native functional operators like map, reduce, filter that take advantage of go's great runtime, and something cool like kotlin's by keyword for delegating ( https://kotlinlang.org/docs/reference/delegation.html)

Re:Just tell me how to use Go Modules by shabbyrobe on Tuesday December 11, 2018 @05:43AM (#18654424)
> Why am I writing all this ceremony (comparatively little compared to other languages, but ceremony nonetheless) when the new, better way is coming?

Whether or not the new way will be better is far from assured.

I'm outing myself as a bit of a "get off my lawn" greybeard here, but as long as such a large swathe of the industry remains trapped in a permanent state of convulsion where everyone competes to build the next monument to their own cleverness, the unprecedented consistency, clarity and productivity that vanilla Go 1 provides will remain under-appreciated.

There's an awful lot of sound and fury over generics and error handling. I sincerely hope that if and when something is done about it, it isn't done in a way that gives anyone a license to be clever.

> "use channels, share memory by communicating"

They walked back a bit from this, thankfully, and I'm seeing less and less nasty "channels for the sake of it" code these days as a result. The party line seems to be closer to "channels are good and bad, mutexes are good and bad, favour whichever is goodest" nowadays, and the larger projects I've worked on have typically been about 50/50 [1]

  [1]: https://github.com/golang/go/wiki/MutexOrChannel
Whenever I think something is missing, I find it helpful to remind myself that Go is kind of the ultimate "80%" experience. Absolutely everything is 80% complete, from the language, to the tools, to the stdlib, it's performance relative to vanilla C... everywhere. 80%. I also think this is the secret of its success.
Re:Just tell me how to use Go Modules by stubish on Tuesday December 11, 2018 @12:35PM (#18656997)
Interesting about channels. I had been wondering why, despite always being touted as a core feature and reason for Go being awesome, I find them quite rare when looking at other people's code. I've only found them particularly useful for communication between goroutines on one project (filtering events, sending them to the correct workers), and the rest of the time mostly used to implement timeouts (and wishing for some syntactic sugar there, since it can be non-obvious how to to avoid leaking the timers).
Re:Just tell me how to use Go Modules by shabbyrobe on Wednesday December 12, 2018 @07:14AM (#18663236)
That's a really good point actually, I hadn't thought directly about this before. I've just evolved a position on it without articulating it directly: I almost never include them in my public APIs, almost never see them in the better APIs, and tend to count it as a strike against a package if I see that they're used without strong justification.

I find there's a lot you have to do manually to handle them correctly and it's easy to miss details. I think they're best cordoned off behind isolated, encapsulated parts of a channel-free public interface.

To walk back a tiny bit from my earlier "80%" claim, I reckon channels are probably more like 50% complete. The existence of "context.Context", itself very much half-baked, is a symptom of that.

You have to think long and hard about cancellation and blocking every time you interact with them (even if you decide you don't ultimately need to), and the results, once you've (hopefully) handled all the corner cases can degenerate into a tangled mess that's hard to reason about, harder to test, and impossible to change. I like to call this kind of code "channel soup".

I'd be interested to see proposals for what the 80% version of channels would look like. What we have now is a great start, but it's not great yet.

Re:Just tell me how to use Go Modules by fpoling on Tuesday December 11, 2018 @04:12PM (#18658935)
Channels in Go do not allow to prioritize messages and proper support for cancelation is highly non-trivial. Both are necessary in complex software. With mutexes one can build all these things, but lack of language support for mutexes sucks.
Re:Just tell me how to use Go Modules by sethammons on Tuesday December 11, 2018 @08:19AM (#18655103)
Why named arguments? The parameters should show up in any decent editor showing their type and the name chosen by the implementor. The only reason I can think of for named arguments is to allow for default values to be left out of function calls, which just wouldn't fit in with Go.
Re:Just tell me how to use Go Modules by jstimpfle on Tuesday December 11, 2018 @11:33AM (#18656459)
The point of named arguments is to be more explicit at the call sites. It's also a way to be more robust against API changes -- you can add new optional named arguments or change their order, without breaking existing invocations. You can use named arguments to make a function interface with many optional arguments -- the kind of thing you would otherwise make a builder style interface for. But that's only the idea; Not saying it is necessarily a good idea to include named arguments.

Many people aren't using "decent text editors". vim isn't one. Webpages and prints of code chunks aren't. Information that you must inquire interactively is simply not the same as static information expressed inline in the code. Such is always a little more typing work but often it's worth it.

Re:Just tell me how to use Go Modules by dcow on Tuesday December 11, 2018 @12:50AM (#18653488)
I would argue `by` and `data` are the two most powerful keywords from any of the modern languages (~last 10 years) maybe next to `go`.
Re:Just tell me how to use Go Modules by dnautics on Tuesday December 11, 2018 @01:01AM (#18653513)
You should check out the "with" keyword from elixir, (but the #1 all time best elixir feature is the |> (pipe) operator combined with IO.inspect)...
Re:Just tell me how to use Go Modules by bluk on Tuesday December 11, 2018 @10:44AM (#18656035)
IMO `async/await` (C# / JavaScript), `defer` (Go), and `guard` (Swift) have changed the structure and flow of programs and are commonly used in their respective languages. Why do you believe that `by` and curiously `data` are so powerful?
Re:Just tell me how to use Go Modules by ngrilly on Tuesday December 11, 2018 @04:48AM (#18654202)
What does `data` do, and in which language?
Re:Just tell me how to use Go Modules by letientai299 on Tuesday December 11, 2018 @05:44AM (#18654429)
I think he means `data` class in kotlin.

https://kotlinlang.org/docs/reference/data-classes.html

Re:Just tell me how to use Go Modules by ngrilly on Tuesday December 11, 2018 @05:58AM (#18654494)
Yes, it looks like this. Thanks!
Re:Just tell me how to use Go Modules by jstimpfle on Tuesday December 11, 2018 @11:38AM (#18656494)
> `data` > modern

Everything old is new again.

Re:Just tell me how to use Go Modules by dcow on Tuesday December 11, 2018 @12:43PM (#18657066)
I was considering `data` specifically in the context of the JVM. I understand we’ve had structs for ages but the concept of anything other than a reference is so terrribly foreign to the JVM it has made so many really shitty programs noticeably less shitty.
Re:Just tell me how to use Go Modules by Groxx on Tuesday December 11, 2018 @12:09AM (#18653391)
There are a couple implementations of this (e.g. staticcheck[1] is probably the biggest and most widely used), but none are really pluggable or easy to interact with. And nearly anything even slightly sophisticated needs to interact with the type checker, which has like one example repo[2] and no other documentation anywhere.

There's a semi-recent experiment to try to make a pluggable lint system[3], but I'm not sure what the current status is.

For my workplace, tbh I plan on making something semi-hacky just to get us off the ground + use real go plugins so we actually have some minor extensibility without reams of boilerplate. I've built several tiny things that just use the AST (which is not bad at all, and quite fast - several thousand source files take about 250ms), and they've been worth the effort like 100x over, but it's hard / impossible to do anything better without the type checker + significant amounts of work.

[1]: very highly recommended, it has far more / better checks than others I've seen, but is also quite slow and hard to extend: https://staticcheck.io/

[2]: it's honestly quite a good repo, but there needs to be more. it's a fairly unfriendly API and there are few helper libs as far as I've seen: https://golang.org/s/types-tutorial

[3]: https://docs.google.com/document/d/1-azPLXaLgTCKeKDNg0HVMq2o...

Re:Just tell me how to use Go Modules by Zach_the_Lizard on Tuesday December 11, 2018 @09:31AM (#18655484)
I second the comments about the various AST and type packages. They are just absurdly unfriendly.

I've written a linter / code formatter / auto bug repair tool internally for my current company and it sucked. Still sucks. Comments doubly so.

The examples are few, undocumented, and don't always work with the vendor directory concept.

I had to write my own custom importer to get vendor support to be more reliable.

I like the concept of shipping with an AST package; it's very forward looking and useful.

I too hope the go/analysis package could be the answer. I believe it has not merged but has a prototype version that conflicts with master. Maybe they'll patch it up and get it landed soon. I hope, anyways

Re:Just tell me how to use Go Modules by Groxx on Tuesday December 11, 2018 @04:44PM (#18659208)
AST for read access I find relatively decent. Straightforward and not complicated.

For manipulating code tho (e.g. for code gen or rewriting), yeah - absurdly unfriendly fits pretty well, and comments only kinda sorta mostly work until you actually try to produce anything polished. It kinda feels like they prioritized forward-compatibility at the cost of usability (which could be a decent decision / a fair tradeoff, but it sucks as a user trying to do stuff now).

Re:Just tell me how to use Go Modules by andreygrehov on Tuesday December 11, 2018 @11:28AM (#18656406)
> How you structure your code beyond the src/git/path/your-code/ is completely up to you and every repo you visit could be different.

Erm...I'm confused. Why on earth every repo should have the same structure? I mean, I understand when you use a framework that prescribes you a layout, but even so, good frameworks try not to get into developer's way. Why the source code of a webapp should have the same structure as a, say, database? There were quite a few comments about standard project layout - I'm sure there is something similar for every language, but these things are just recommendations. And thank god they are not rules. Am I missing something here?

Re:Just tell me how to use Go Modules by oblio on Thursday December 13, 2018 @09:26AM (#18672469)
Yes. Having a common overall structure is good.

When I open a Maven project, the Java sources are always (99,99% of the time) in src/main/java, the resources are in /src/main/resources, the Java test sources are in src/test/java, etc.

Every project has to have source code, every project could have resources. Every project could have test sources, etc.

Re:Just tell me how to use Go Modules by Memosyne on Tuesday December 11, 2018 @06:25AM (#18654601)
I try to adhere to https://github.com/golang-standards/project-layout, but I find that most repositories just have their own way of doing things. I really wish Go provided a standard structure for organizing projects, as I find it increasingly difficult to understand dozens of different structures.
Re:Just tell me how to use Go Modules by alias_neo on Tuesday December 11, 2018 @04:57AM (#18654236)
When i first started Golang professionally, over 3 years ago, my first gripe was the GOPATH.

WHY SHOULD ALL MY CODE SIT IN ONE FOLDER? I say gripe, it was a huge annoyance. Why on earth, also, should i need to use the repo path in my structure and imports? It's obscene.

My next annoyance was the utter inability to easily parse dynamic JSON. But you know what the JSON looks like? Great, you'll only have to wrap it in a few structs and check your capitalisation if you want it to parse at all, no problem you think before half a day has gone just parsing one response.

The vendoring makes my month painful as I'm retooling the build system for the flavour of the month vendoring toy.

Structuring large projects still causes me great pain.

All of that said, Golang is now my favourite language to program in, and is my default for any new project.

Re:Just tell me how to use Go Modules by dom96 on Tuesday December 11, 2018 @05:59AM (#18654503)
I've had a similar reaction when trying Go. While I'm biased, I strongly believe Nim offers a much better alternative which solves all of the problems you mention in a much better way.

In particular, parsing JSON is a breeze, just like in Python. And there is no GOPATH weirdness.

Give Nim a try if you haven't already, it may just become your new favourite language :)

Re:Just tell me how to use Go Modules by alias_neo on Tuesday December 11, 2018 @06:39AM (#18654655)
I'll look into it, thanks. I was planning on Rust being my next language but I'll see what Nim offers. I take it your bias is because your the/a creator?
Re:Just tell me how to use Go Modules by dom96 on Tuesday December 11, 2018 @07:01AM (#18654746)
I'm one of the core developers
Re:Just tell me how to use Go Modules by alias_neo on Tuesday December 11, 2018 @08:28AM (#18655151)
Ok! What's its standard library like compared to Go? I like that I can write powerful projects with few external dependencies. I don't like that C binding is slow and C++ binding is missing.

How does it compare to Rust, which is currently top of my list to learn next?

Can you suggest a reason, or a few why Nim should knock Rust from that spot?

Re:Just tell me how to use Go Modules by sjellis on Tuesday December 11, 2018 @03:34AM (#18653960)
> Golang doesn't feel like a language that wants to prescribe a lot more than fundamentals. How you structure your code beyond the src/git/path/your-code/ is completely up to you and every repo you visit could be different.

I only follow Go casually ATM, but I think that it might fairer to say that the core team wants to stay focused on delivering the language and implementations, so we get blog posts and conference talks, but not a big trove of guidance on patterns & practices.

The best place to interact with the wider Go community is the Gophers Slack, and if you visit there, you will find that there's a lot of knowledge and thinking, but it's in scattered places. For example, this article will probably be cited in any discussion of application structure: https://medium.com/@benbjohnson/standard-package-layout-7cdb...

Re:Just tell me how to use Go Modules by AlexCoventry on Tuesday December 11, 2018 @10:11AM (#18655778)
> es lint style tool that allows experts to share their best practices via plugins,

Have you seen gometalinter? Its defaults are way too computationally expensive for me, but it brings together reports from a lot of different linters.

https://github.com/alecthomas/gometalinter#supported-linters

Re:Just tell me how to use Go Modules by jcelerier on Tuesday December 11, 2018 @04:54AM (#18654222)
> in the "this is how you should unpack dynamic json content" sense.

I would be very wary of a language who has core features around such transient things than JSON unpacking.

Re:Just tell me how to use Go Modules by InGodsName on Tuesday December 11, 2018 @05:27AM (#18654340)
Our agency most mostly does SaaS apps.

We use Rails and React.

We tried Go for backend but Go is missing ActiveRecorss, payment gateway libraries, etc...

Where is migration management like sqlachemy or activ records?

For building SaaS product i think you should choose Rails.

It takes only few minutes to creare subscription billling, team accounts, invoice etc.. functionality.

I would like to know what you guys use.

Re:Just tell me how to use Go Modules by bpizzi on Tuesday December 11, 2018 @05:48AM (#18654444)
Well, the comparison is not really fair, Rails is a framework for building webapp, whereas Go is a language with a standard library.

To simplify a bit: Go is in the same bag as C++/C#/Java, Rails is in the same bag as PHP & [Symfony/Laravel].

If you're mass building CRUD web applications against customers requirements then, clearly, RoR is the best tool for the job (versus Golang).

Re:Just tell me how to use Go Modules by lowercased on Tuesday December 11, 2018 @09:39AM (#18655530)
> Go is in the same bag as C++/C#/Java, Rails is in the same bag as PHP & [Symfony/Laravel].

Go is in the same bag as C++/C#/Java/Ruby/PHP (languages)

Rails is in the same bag as Symfony/Laravel (frameworks)

Re:Just tell me how to use Go Modules by bpizzi on Tuesday December 11, 2018 @10:36AM (#18655952)
I literally said 'Rails is a framework for building webapp, whereas Go is a language with a standard library'. what's your point?
Re:Just tell me how to use Go Modules by lowercased on Tuesday December 11, 2018 @03:24PM (#18658543)
Ruby is a language distinct from the Rails framework.

PHP is a language distinct from the other frameworks listed.

Small nitpick, but I always hate to see conflation of languages and frameworks when making comparisons.

Re:Just tell me how to use Go Modules by bpizzi on Thursday December 13, 2018 @09:38AM (#18672558)
For the record, I was not confusing anything here, it was you thinking that I was first listing languages then frameworks, which was not the case.
Re:Just tell me how to use Go Modules by sixstringtheory on Tuesday December 11, 2018 @01:21PM (#18657417)
They moved PHP from framework to language, it’s a small nit but worth picking I suppose.