希望 Go 1.5 的 go/printer 能支持所谓的 MiniGo.

310 views
Skip to first unread message

YU HengChun 喻恒春

unread,
Mar 16, 2015, 1:32:26 AM3/16/15
to golan...@googlegroups.com
大家知道 JavaScript 语言有 mini 工具, 可以把 js 源码精简为一行.
希望 Go 的标准库也能提供这样的支持, 比如直接在 go/printer 中支持.
这里阐述一下原因, 一个典型的应用场景, 翻译 Go 包时会可能发生这种情况:

因翻译的需要或者译者改动了非注释代码的排版格式.

通过程序判断这种改动是否是无害的, 只是排版的不同, 这是有现实价值的.
现有的(1.4) Go 标准库还无法提供这种支持, 除非去分析 AST, 那样成本很高.
我在 go1.5.txt 中看到有关于 semicolon 的增进支持. 所以希望能更进一步, Go 1.5 的 go/printer 能支持所谓的 MiniGo.

MiniGo 指的是: 格式化输出一个 Go AST 节点, 剔除用于排版的空格, 缩进, 换行并补全分号.

我在 http://play.golang.org/p/q8RXOHJgto 写了个演示, 简单的说明区别.

事实上根据 spec#Semicolons 所讲, Go 的工具链内部已经做了相关工作, 希望能导出相关功能给开发者使用.

Fino

unread,
Mar 17, 2015, 12:21:56 AM3/17/15
to golan...@googlegroups.com
hi hengchun,

I think these sentence are not difficult in English,  u can make it, if u spend sometime,

BR-fino

在 2015年3月16日星期一 UTC+8下午1:32:26,YU HengChun 喻恒春写道:

minux

unread,
Mar 17, 2015, 1:22:23 AM3/17/15
to YU HengChun 喻恒春, golang-nuts
I don't think we should introduced the so-called "minified Go" (similar to minified Javascript)
option to go/printer (or go/format).

What you really want is to compare two ASTs for equivalence while ignoring the comments.
there is currently no standard package function to do that, but that functionality doesn't need
to be in the standard package either.

Jiade Dai

unread,
Mar 17, 2015, 7:03:32 AM3/17/15
to golan...@googlegroups.com
javascript会有mini工具是因为压缩后js文件更小,方便在网络中快速传输。
但依据golang的主创们的观点----压缩后还怎么读啊,老兄!
gofmt这个排版工具不就没有用了吗?
golang是编译型的吧!

YU HengChun 喻恒春

unread,
Mar 17, 2015, 10:01:04 AM3/17/15
to golan...@googlegroups.com


在 2015年3月17日星期二 UTC+8下午7:03:32,Jiade Dai写道:
Jiade Dai:
这个的目的举例:

一段代码, 经过不同的排版格式或者修改注释后, Go 代码实体其实是没有变化的.
因  "minified Go" 剔除了注释以及排版差异, 就可以判断出修改者实质上是没有修改代码实体的.

一个典型的应用场景是注释文档翻译, 有了  "minified Go" 就可以用程序判断代码实体是否被修改过.
如果没有那就可以进行下一步提交之类的行为了.

简单的排除空白肯定是不行的, 还要补全分号以及处理 `strings...` 这种可能包含换行的字符串.
偶英文不够好, 怕用英文表达错误.
如果可以的话, 请帮忙用英文表述下这个需求吧.
虽然 minux 前述认为这种功能不应该出现在 standard package 中.
很明显完成这个功能需要触动 go/parser 和 go/printer, 甚至和 go/scanner 的行为也有关,
如果不能被 standard package 支持, 那么日后的维护将是枯燥乏味的跟进 standard package, 甚至还有版本差异问题.
所以希望能被 standard package  支持.

Yucong Sun

unread,
Mar 17, 2015, 10:19:05 AM3/17/15
to YU HengChun 喻恒春, golan...@googlegroups.com
I think what you are asking is not what you want to do : You are
asking about how to get a an deterministic AST for a given go code, so
you can ignore the comment changes, even function reordering etc.

I think what you want is in http://golang.org/pkg/go/parser/#ParseFile
. Use that to get a AST for the file , then you probably need to
also sort the resulting AST . Then find a way to compute a hash for
the AST data structure . Next time do the samething and you will be
able to see if something meaningful has been changed in the source
code.

Cheers.
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Egon

unread,
Mar 17, 2015, 10:30:37 AM3/17/15
to golan...@googlegroups.com


On Tuesday, 17 March 2015 16:01:04 UTC+2, YU HengChun 喻恒春 wrote:


在 2015年3月17日星期二 UTC+8下午7:03:32,Jiade Dai写道:
javascript会有mini工具是因为压缩后js文件更小,方便在网络中快速传输。
但依据golang的主创们的观点----压缩后还怎么读啊,老兄!
gofmt这个排版工具不就没有用了吗?
golang是编译型的吧!


Jiade Dai:
这个的目的举例:

一段代码, 经过不同的排版格式或者修改注释后, Go 代码实体其实是没有变化的.
因  "minified Go" 剔除了注释以及排版差异, 就可以判断出修改者实质上是没有修改代码实体的.

一个典型的应用场景是注释文档翻译, 有了  "minified Go" 就可以用程序判断代码实体是否被修改过.
如果没有那就可以进行下一步提交之类的行为了.

YU HengChun 喻恒春

unread,
Mar 17, 2015, 10:41:08 AM3/17/15
to golan...@googlegroups.com
Yucong Sun:

我确实想要通过  "minified Go" 的方法来判断 go 代码实体(排除注释和空白符号那些) AST 是否变化了.
不过有些原因让我感觉应该从  "minified Go" 入手, 而不是遍历比较 AST.
首先要完整的遍历 AST 并比较, 需要打开两个文件, 而且整个代码写下来代码量几乎比得上 go/printer 了.
其次如果从翻译注释这个应用场景来说, 完全不需要保留前一个版本的文件, "minified Go" 后做个 MD5 求值.
修改后, 再做个 MD5 对比下就可以了. 这比遍历比较 AST 方便些.
感谢你关注这个话题.

在 2015年3月17日星期二 UTC+8下午10:19:05,Yucong Sun写道:
Reply all
Reply to author
Forward
0 new messages