Go+ 发布 weekly release: v0.7.3
七月份可能是今年以来我个人最忙的一个时间段,所以上一个 Go+ 的 weekly release: v0.7.2 延迟了一天发布,并且也没来得及将发布的内容整理成文。这周我们就把这两周的更新合并在一起说。
首先是一个影响颇大的工作细节改变。之前我们合并 pull request 的时候,是原样合并,这样 pull request 在实现某功能或修改某个 bug 时进行了 N 次 commits,我们合并的时候就会生成 N+1 次 commits。但是现在我们合并 pull request 改为 “Squash and merge”,简单说就是把这 N+1 次 commits 合并为 1 次 commits。
这样做的好处是阅读源代码的时候比较方便,历史提交记录中没有多余的 “废话”,哪些功能修改涉及了哪些文件的修改,一清二楚。当然不好的地方也是有的。一个是冲突比之前更容易发生了。另一个是看 Github Contributors 信息的时候,会发现 commits 次数比之前差不多整整少了一个数量级(每个 commit 变成了一个 big commit 了)。
回到我们的变更。在 v0.7.2 中,我们最重要的是发布了一个叫 Auto property 的功能。这个功能在很多场景下会让 Go+ 的代码看起来更优雅,尤其是在 DOM Query 场景。
所谓 DOM Query,是指 DOM 树查询场景。我个人不建议采用 XPath 这种 DSL 文法进行查询,而是直接采用语言自身的语法能力来表达查询,比如:
import "github.com/goplus/gop/ast/goptest"doc := goptest.New(`... Go+ code ...`)!
println(doc.Any().FuncDecl().Name())
这是在没有 Auto property 是我们的查询语法,它表示我们读取了 Go+ 的语法树,并且找到所有的函数,并打印它们的名字。在有了 Auto property 后,这段代码变成了:
import "github.com/goplus/gop/ast/goptest"doc := goptest.New(`... Go+ code ...`)!
println(doc.any.funcDecl.name)
我们知道很多语言都有 property 这样的概念。property 往往会有 get 和 set 方法。如果只有 get 方法,那么就是只读的 property。那么究竟什么是 Auto property?它其实是一种只读 property,只是无需自己实现 get 方法。简单来说,如果一个类型拥有一个无参数的 Bar() 方法,那么在 Go+ 中就自动拥有了一个名为 bar 的 property。这样,foo.bar 其实就等价于 foo.Bar()。
在 weekly release v0.7.3 中,最重要的变化是新增加了 iGo+。也就是说,现在 Go+ 可以运行在 REPL/Console 模式下了。我很期待下一个 weekly release 能够把它搬到 Web 上。
这一周我们还提出了 Go+ 的 lambda 函数的设计。尽管 Go 语言的 closure 挺好,但是相对于 Data Science 而言,表达上还是比较丑陋的。我们拿 Map/Reduce 作为例子:
foo.Map(func(x float64) float64 { return x*x })
但是在 Go+ 中我们希望它变成:
foo.Map(x => x*x)
如果你希望了解更多的细节,请参见 https://github.com/goplus/gop/issues/321。
以下是完整的 Release Notes:
Design of Go+ lambda functions
See #321. Comments are welcome.
Commands
gop repl (ie. igop)
Run Go+ in REPL/Console mode. Thanks contribution of jiangz222 ([email protected]).
Defer
Currently only available in bytecode backend. Thanks contribution of go-wyvern ([email protected]).
Bugs fix
Variable scope bug: variable redefinition.
Array literal is deduced as a pointer.
Fix inconsistent command line output for different help commands.
cmd/qexp: fix export mod pkg with version (#501).
Thanks contribution of JessonChan ([email protected]), visualfc ([email protected]), si9ma.
Git merge mode
We change merge mode to Squash and merge. It will combine all commits from the head branch into a single commit in the base branch.
Auto property
Let's see an example written in Go+:
import "github.com/goplus/gop/ast/goptest"doc := goptest.New(`... Go+ code ...`)!
println(doc.Any().FuncDecl().Name())
property who has get and set methods.Suppose we have get property, the above example will be:
import "github.com/goplus/gop/ast/goptest"doc := goptest.New(`... Go+ code ...`)!
println(doc.any.funcDecl.name)
auto property. It is a get property, but is implemented automatically. If we have a method named Bar(), then we will have a get property named bar at the same time.Go+ related packages
github.com/goplus/gop/ast/gopq
github.com/goplus/gop/ast/goptest
See tutorial/22-Auto-Property.
Flow control
break/continue/return in
for rangestatements.
Thanks contribution of JessonChan ([email protected]).
Misc
Simplify test cases of cl/stmt_test.go.
Command
gop exportbug fixed.Getting value from a map panics if the key not exists.
Export
fmtpackage.Update dependency: github.com/qiniu/x v1.11.5
Thanks contribution of JessonChan ([email protected]), visualfc ([email protected]).