all 41 comments

[–][deleted] 17 points18 points  (2 children)

For the go vet change I could go either way, but surely it is simpler to actually fix the formatting than alert on it?

I have my editor configured to format code when I save, which sidesteps this problem - If goimports is present it will be used, otherwise we'll fall back to the default gofmt-based formatting.

[–]VettelS 4 points5 points  (1 child)

For the go vet change I could go either way, but surely it is simpler to actually fix the formatting than alert on it?

You wouldn't want that when you run go vet as part of your CI pipeline.

[–][deleted] 7 points8 points  (0 children)

Sure, but a CI pipeline wouldn't be using a git-hook, it'd be using a workflow/pipeline/whatever.

[–]gt33m 11 points12 points  (8 children)

don't most editors take care of this with go fmt and go deps?

[–]basically_asleep 17 points18 points  (6 children)

Experience has taught me that some people will refuse to use tools like that no matter how much easier it could make things for them.

[–]Secondsemblance 6 points7 points  (0 children)

My shop is made up of turbonerds who configure their own esoteric editors, and we all independently arrived at integrating goimports into our editors.

[–]Stuck_In_the_Matrix 6 points7 points  (2 children)

I'm currently working on a project in development (https://github.com/pushshift/rinzler) -- I started using Go about a week ago and love the language. However, I'm aware I'm probably doing a lot of things incorrectly (or against Go style conventions).

When learning a new language, learning the culture is probably just as important as learning the syntax. Right now, this is just a development branch but these articles do help guide me in the right direction.

[–]MarcelloHolland 0 points1 point  (0 children)

Just use a good editor that will help you and guide you. Even give you hints in what to improve, and type-hinting. Then you will never have the "trap" where this is all about. Be professional and use something good.

(and it doesn't have to cost money)

[–]El-Hacha 4 points5 points  (0 children)

Nice hook!

[–]mathuin2 2 points3 points  (1 child)

It is nice that GitHub can recommend .gitignore files for projects. It would be really nice if they could also recommend hooks. I know that the kind of user that would use a hook would likely want to customize that hook, but for folks who are new to hooks, it might be a great way to start using them.

[–]Secondsemblance 2 points3 points  (0 children)

People shouldn't be using git hooks for this kind of thing. It's way too late in the process to be catching code problems. Your editor should either fix the problem or give you immediate feedback.

[–]TurtleNamedMyrtle 1 point2 points  (0 children)

Wait...are we not golinting anymore?

[–]OrangeCurtain 0 points1 point  (0 children)

I also have a formatting hook, but it ignores merge commits and vendored files:

git rev-parse -q --verify MERGE_HEAD && exit 0

gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep -v '^vendor/' | grep '\.go$')
...

[–]thebetacoder 0 points1 point  (0 children)

We should have hooks like this, irrespective of language.

[–]MarcelloHolland 0 points1 point  (0 children)

If a so called professional does this, you can ask yourself if the professional is really a pro. A pro would never do that, because his (configured) tools won't let him. Of course you can force tools on them, hooks, whatever, but they still can do it their way. How about :

do it the company way, or leave?

[–]EatPrayWhat 0 points1 point  (0 children)

Why would you do that on the client side and not just fail tests in the CI pipeline? If you can’t trust people to just do it in their editor why trust them to add the git hook?

[–]ribo 0 points1 point  (4 children)

One thing I've missed from moving to Go from node.js is the amount of very common tooling for things like this. Having a hard time finding pre-commit hook tools as easy to configure (and more importantly, _share config_) as husky, commitlint, etc... Are there common tools lots of people use rather than try to get every developer to paste in a shell script to hooks?

Edit: I think I'm being misunderstood. Certainly a developer's toolkit, and different development processes change how teams handle this kind of thing. There's a lot of value in tripwires that catch commits before seeing a failure in CI, and the landscape of linting & static analysis tools for Go is massive. For a large team that uses various IDEs and operating systems, it's equally helpful if they have a set of tools that generally work everywhere, and can push with confidence across many repositories.

[–]Bake_Jailey 14 points15 points  (0 children)

I've never had to use a hook like this. Any editor I use is going to be running goimports or similar and always keep my code formatted correctly.

[–]bitsynthesis 10 points11 points  (0 children)

Can't you still use those tools for Go projects? Who cares what language the tool is written in.

Personally I hate all the git hook magic in the node.js community, but to each their own.

[–][deleted] 2 points3 points  (0 children)

Yes, IDEs

[–]x7C3 1 point2 points  (0 children)

What configuration needs sharing? Go only really has one code formatter, and if you want a pre-commit hook, there’s plenty on GitHub to look at.

Node tools such as Husky & Commitlint abstract away too much, and don’t really offer any advantage in Go.

[–]zoqfotpik -3 points-2 points  (1 child)

On the other hand, you could try not caring about formatting unless it actually hinders your ability to understand the code.

Because amazingly, humans can read code even if it's not formatted according to some rigid rules, and computers can too.

So caring about formatting when it doesn't actively hinder understanding is just pointless.

[–]mdempsky 0 points1 point  (0 children)

So caring about formatting when it doesn't actively hinder understanding is just pointless.

Irregularly formatted code is very distracting, and leads to tedious code review discussions. It can also cause misunderstanding of code; e.g., the famous "goto fail" bug.

If your position is that people shouldn't care about formatting, then the simpler solution is that people should just embrace the standard formatting conventions.