<p>So I just got into Go, installed it, opened Atom and installed go-plus package for some IDE utils related to the language. Everything was cool that evening, I learned to code a bit, indented my code with tabs, and was happy about it. So I wanted to share it with my friends but when I pushed my code to GitHub I saw this disaster: <a href="https://i.imgur.com/RSWwrDh.png" rel="nofollow">https://i.imgur.com/RSWwrDh.png</a> </p>
<p>All my tabs on GitHub look like they are in non-configured Vim, with 4 spaces? (I expected 2 as it gets formated in other IDEs) I didn't have this issue with any language so far...is this Atom issue? Go-plus plugin? GitHub displays tabs differently for Golang? Has anyone had similar issue before?</p>
<hr/>**评论:**<br/><br/>pdffs: <pre><p>Is that spaces, or tabs? The standard for Go (as produced by <code>gofmt</code>) is to use tabs for indentation, not spaces.</p>
<p>I feel like maybe you are using the word <code>tab</code> to describe indentation, but <code>tab</code> is a character.</p></pre>kunokdev: <pre><p>I pressed tab (the button) when I needed indention (like all normal people). I would press enter in Atom and it would auto-indent. Just checked again, it's actually 1 tab character in GitHub, but that's really ugly to read. So perhaps it's due to GitHub? Oh just went to check other Go repos on GitHub and they are all like that :'(</p></pre>pdffs: <pre><p>Tabs are superior to spaces for indentation, and are the standard for Go, you'll get used to it.</p></pre>kunokdev: <pre><p>Well I do use tabs since beginning, its just the way GitHub shows this code is gross</p></pre>jussij: <pre><p>The actual size of a tab is dependent on the thing that is viewing the tab.</p>
<p>In this case GitHub is using the <em>standard default tab size</em> which is 8 characters.</p>
<p>For example, if you print you code it will most likely also print with that 8 character tab size as most printers define a tab as 8 charters in size.</p></pre>App1eNerd: <pre><p>Add a .editorconfig with indent_size of 4.</p></pre>gbrlsnchs: <pre><p>There's nothing wrong with the output. <code>go-plus</code> runs <code>go fmt</code> when you save <code>*.go</code> files, which formats the code to use tabs. GitHub code preview sets tab length to 8, probably respecting Go code style.</p></pre>daveddev: <pre><p>If you use a tool like Tampermonkey, the tab size can be controlled:</p>
<pre><code>// ==UserScript==
// @name Control tabs
// @namespace https://www.codemodus.com/
// @version 0.1
// @description Change the visual size of tabs in github.
// @author daved
// @match https://github.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
function tabWidthPref(ext) {
if (ext.length == 0) return 0;
var prefs = {
'.go': 4,
'.cnf': 8,
'.js': 2,
'.html': 2,
'.htm': 2,
'.css': 2,
};
var p = prefs[ext];
return (p < 1) ? 4 : p;
}
function setTabSizes(size) {
if (size < 1) return;
var es = document.getElementsByClassName("tab-size");
for (var i = 0; es[i]; i++) {
es[i].setAttribute("data-tab-size", size);
}
}
function extension() {
var path = window.location.pathname;
var i = path.lastIndexOf('.');
return (i < 0) ? '' : path.substr(i);
}
function waitForResource(state, fn) {
if (!state.includes("/blob/")) return;
fetch(state).then(function(response) {
setTimeout(fn, 333);
});
}
(function(history){
var ps = history.pushState;
history.pushState = function(state) {
if (typeof history.onpushstate == "function") {
history.onpushstate({state: state});
}
return ps.apply(history, arguments);
};
})(window.history);
var set = () => setTabSizes(tabWidthPref(extension()));
var waitSet = () => waitForResource(history.state, set);
window.onpopstate = history.onpushstate = waitSet;
set();
})();
</code></pre></pre>
Go lang, Atom, tabs and go-plus in combination produce tabs worth 4 spaces on GitHub code preview
agolangf · · 540 次点击这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传