IT资讯 Go+ 1.0 正式发布 & Go+ 发展路线图公布

allison · 2021-10-18 09:00:07 · 热度: 15

10 月 15 日,七牛云 CEO、Go+ 语言发明人许式伟与 Go+ 语言贡献者共同发布了 Go+ 1.0 正式版本,并公布了 Go+ 发展路线图。

Go+ 于 2020 年由许式伟面向全球推出,定位是一门为数据科学而生的编程语言。在经历了一年多的开发后,终于发布了 1.0 正式版本。

根据 Go+ 的 Readme 描述,它是一门适用于工程、STEM 教育和数据科学的编程语言。主要特性包括:

  • 静态类型语言。
  •  Go 完全兼容。
  • 脚本化的风格,以及比 Go 更易于阅读的数据科学代码。
  • 支持字节码后端和 Go 代码生成。在字节码模式下,Go+ 不支持 cgo。然而,在 Go 代码生成模式下,Go+ 完全支持 cgo

Go+ 部分语言特性

映射字面量

x := {"Hello": 1, "xsw": 3.4} // map[string]float64
y := {"Hello": 1, "xsw": "Go+"} // map[string]interface{}
z := {"Hello": 1, "xsw": 3} // map[string]int
empty := {} // map[string]interface{}

切片字面量

x := [1, 3.4] // []float64
y := [1] // []int
z := [1+2i, "xsw"] // []interface{}
a := [1, 3.4, 3+4i] // []complex128
b := [5+6i] // []complex128
c := ["xsw", 3] // []interface{}
empty := [] // []interface{}

列表推导

a := [x*x for x <- [1, 3, 5, 7, 11]]
b := [x*x for x <- [1, 3, 5, 7, 11], x > 3]
c := [i+v for i, v <- [1, 3, 5, 7, 11], i%2 == 1]
d := [k+","+s for k, s <- {"Hello": "xsw", "Hi": "Go+"}]

arr := [1, 2, 3, 4, 5, 6]
e := [[a, b] for a <- arr, a < b for b <- arr, b > 2]

x := {x: i for i, x <- [1, 3, 5, 7, 11]}
y := {x: i for i, x <- [1, 3, 5, 7, 11], i%2 == 1}
z := {v: k for k, v <- {1: "Hello", 3: "Hi", 5: "xsw", 7: "Go+"}, k > 3}

从集合中选择数据

type student struct {
    name  string
    score int
}

students := [student{"Ken", 90}, student{"Jason", 80}, student{"Lily", 85}]

unknownScore, ok := {x.score for x <- students, x.name == "Unknown"}
jasonScore := {x.score for x <- students, x.name == "Jason"}

println(unknownScore, ok) // output: 0 false
println(jasonScore) // output: 80

检查数据是否存在于集合中

type student struct {
    name  string
    score int
}

students := [student{"Ken", 90}, student{"Jason", 80}, student{"Lily", 85}]

hasJason := {for x <- students, x.name == "Jason"} // is any student named Jason?
hasFailed := {for x <- students, x.score < 60}     // is any student failed?

For 循环

sum := 0
for x <- [1, 3, 5, 7, 11, 13, 17], x > 3 {
    sum += x
}

For range of UDT

type Foo struct {
}

// Gop_Enum(proc func(val ValType)) or:
// Gop_Enum(proc func(key KeyType, val ValType))
func (p *Foo) Gop_Enum(proc func(key int, val string)) {
    // ...
}

foo := &Foo{}
for k, v := range foo {
    println(k, v)
}

for k, v <- foo {
    println(k, v)
}

println({v: k for k, v <- foo})

注意:对于 udt.Gop_Enum(回调)的范围,无法使用 break/continue 或 return 语句。

For range of UDT2

type FooIter struct {
}

// (Iterator) Next() (val ValType, ok bool) or:
// (Iterator) Next() (key KeyType, val ValType, ok bool)
func (p *FooIter) Next() (key int, val string, ok bool) {
    // ...
}

type Foo struct {
}

// Gop_Enum() Iterator
func (p *Foo) Gop_Enum() *FooIter {
    // ...
}

foo := &Foo{}
for k, v := range foo {
    println(k, v)
}

for k, v <- foo {
    println(k, v)
}

println({v: k for k, v <- foo})

Lambda 表达式

func plot(fn func(x float64) float64) {
    // ...
}

func plot2(fn func(x float64) (float64, float64)) {
    // ...
}

plot(x => x * x)           // plot(func(x float64) float64 { return x * x })
plot2(x => (x * x, x + x)) // plot2(func(x float64) (float64, float64) { return x * x, x + x })

重载运算符

import "math/big"

type MyBigInt struct {
    *big.Int
}

func Int(v *big.Int) MyBigInt {
    return MyBigInt{v}
}

func (a MyBigInt) + (b MyBigInt) MyBigInt { // binary operator
    return MyBigInt{new(big.Int).Add(a.Int, b.Int)}
}

func (a MyBigInt) += (b MyBigInt) {
    a.Int.Add(a.Int, b.Int)
}

func -(a MyBigInt) MyBigInt { // unary operator
    return MyBigInt{new(big.Int).Neg(a.Int)}
}

a := Int(1r)
a += Int(2r)
println(a + Int(3r))
println(-a)

对于 Go+ 的发展路线,根据 Go+ 语言贡献者代表陈东坡的介绍,Go+ 作为一门能够正式应用于工程与数据的编程语言,从设计理念到语言特性均紧紧围绕“三位一体”,融合工程开发的 Go、数据科学的 Python 和教学领域的 Scratch(“以 Python 之形结合 Go 之心”),让 程序员 与数据科学家沟通无碍,使初学者更易入门。

陈东坡表示 Go+ 团队的规划是第一步是实现工程能力(兼容 Go)和教学能力(兼容 scratch)的融合,而数据科学能力的建设则仍需要长期的努力,目前计划暂定于 2023 年发布 Go+ 2.0 版本,支持导入 Python 包,实现 Python 生态的引入与兼容。

猜你喜欢:
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册