重新调整代码以提高可读性。

huangapple go评论71阅读模式
英文:

realign code for readbility

问题

我有一些代码,看起来像这样:

Id string // 由交换提供的唯一竞标请求ID
Fields []interface{} // 字段对象数组
User interface{} // 用户对象
Device interface{}

我知道align-regexp函数,并且我使用它来对齐我的注释。这将输出类似于:

Id string            // 由交换提供的唯一竞标请求ID
Fields []interface{} // 字段对象数组
User interface{}     // 用户对象
Device interface{}

然而,我也希望类型能够正确对齐。我试图弄清楚如何使用align-regexp来实现这一点。不幸的是,我的正则表达式技能有限。

我希望输出的结果如下所示:

Id     string        // 由交换提供的唯一竞标请求ID
Fields []interface{} // 字段对象数组
User   interface{}   // 用户对象
Device interface{}

如果这需要两个单独的函数调用,我也不介意,因为我可以编写一个函数来封装它们。

另外,如果解决方案最终使用align-regexp,我也不在意。迄今为止,这是我用来处理这类问题的方法。

英文:

I have some code that looks like

Id string // uniq bid req id provided by the exchange
Fields []interface{} // array of field objects
User interface{} // user obj
Device interface{}

I know about the align-regexp function, and I use this to align my comments. This would output something like

Id string            // uniq bid req id provided by the exchange
Fields []interface{} // array of field objects
User interface{}     // user obj
Device interface{}

However, I would also like the types to be aligned properly. I was trying to figure out how I would be able to use align-regexp in order to accomplish this. Unfortunately, my regex skills are lacking.

I would like the output to look like

Id     string        // uniq bid req id provided by the exchange
Fields []interface{} // array of field objects
User   interface{}   // user obj
Device interface{}

I wouldn't mind if this was two separate function calls, as I could just write a function that could encapsulate them.

Also, I don't care if the solution ends up using align-regexp. This is just what I have been using for things like this thus far.

答案1

得分: 1

这可能比你想象的要简单。

你的类型总是一个单词吗?那么你可以使用一个空格作为第一个正则表达式,然后在“//”上对齐。在这个例子中是有效的 重新调整代码以提高可读性。

英文:

It's maybe simpler than you think.

Are your types always one word ? Then you can use a space as the first regexp, and then align on the //. It does work with this example 重新调整代码以提高可读性。

huangapple
  • 本文由 发表于 2014年9月19日 21:54:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/25935646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定