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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Refactoring with go fmt

86 点作者 spf13超过 11 年前

9 条评论

simias超过 11 年前
As a C coder I really wish it had a standard syntax and a similar tool to deal with that. Dealing with proper style is a constant headache among a team. And I always feel like a &quot;coding style nazi&quot; when I bother other people to tell them &quot;you should put spaces here&quot;, &quot;your braces are not in the correct position&quot;, &quot;you shouldn&#x27;t use TABs there&quot; etc...<p>So in the end I stop doing it (or I let small mistakes go by) and soon enough the whole codebase is heterogeneous and looks like crap everywhere.<p>I don&#x27;t really care about Go and never used it but this is a very good move on their part IMO. No bickering allowed, it&#x27;s standard. No arguing, just run &quot;go fmt&quot; and you&#x27;re set. Brilliant.
评论 #6725415 未加载
评论 #6728284 未加载
评论 #6725400 未加载
评论 #6725363 未加载
cdoxsey超过 11 年前
The Go Sublime plugin will go fmt your code on save:<p><a href="https://github.com/DisposaBoy/GoSublime" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;DisposaBoy&#x2F;GoSublime</a><p>It&#x27;s nice not having to think about proper spacing anymore and just focusing on writing the code itself.
评论 #6725740 未加载
评论 #6725700 未加载
sillysaurus2超过 11 年前
<i>Go fmt isn’t without it’s detractors. People complain that go fmt isn’t customizable and that it puts braces where they don’t want them. Fools.</i><p>How would you feel if I left a comment calling you a fool for writing such a remark? Now realize you&#x27;re giving that same feeling to every reader you&#x27;re trying to convince.<p>It&#x27;s far more effective to point out why someone is mistaken and leave it at that, as &#x27;pg has demonstrated over the years.
评论 #6725062 未加载
评论 #6725200 未加载
评论 #6726738 未加载
评论 #6725665 未加载
评论 #6726287 未加载
motter超过 11 年前
`go vet` (checks correctness) and golint (<a href="https://github.com/golang/lint" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;golang&#x2F;lint</a>) are also extremely useful.<p>As a whole the toolchain makes working with Go code very convenient.
评论 #6728101 未加载
评论 #6726740 未加载
chewxy超过 11 年前
You can even use gofmt to export variables that you want exported throughout the project. I wrote about it on this coderwall: <a href="https://coderwall.com/p/ds3iuw" rel="nofollow">https:&#x2F;&#x2F;coderwall.com&#x2F;p&#x2F;ds3iuw</a><p>edit: aha, the article did mention &quot;gofmt -r&quot;
vph超过 11 年前
This is nice, but I don&#x27;t think it is refactoring.
评论 #6725986 未加载
chimeracoder超过 11 年前
&gt; People complain that go fmt isn’t customizable and that it puts braces where they don’t want them.<p>I assume this is referring to putting opening braces on the next line.<p>IIRC, this has nothing to do with the `go fmt` tool - unlike in C, this is actually a syntax error in Go: <a href="http://play.golang.org/p/bn30UNkr1z" rel="nofollow">http:&#x2F;&#x2F;play.golang.org&#x2F;p&#x2F;bn30UNkr1z</a><p>One of the reasons for this decision is because it helps enforce a truly context-free grammar for the language.<p>Ironically, the fact that Go has a nearly context-free grammar is what allows tools like `go fmt` and the obsolete `go fix` to make modifications to the code deterministically, with guarantees that it will not change the behavior of the code.
评论 #6725555 未加载
pjmlp超过 11 年前
While go fmt is great tool, it is no different than integrating indent or similar tool in the source control system.
评论 #6725860 未加载
CmonDev超过 11 年前
&#x27;golang&#x27; would me more appropriate in the subj. I thought it was something generic
评论 #6726144 未加载