Go+ 1.0 正式釋出 & Go+ 發展路線圖公佈

2021-10-18 09:00:07

10 月 15 日,七牛雲 CEO、Go+ 語言發明人許式偉與 Go+ 語言貢獻者共同釋出了 Go+ 1.0 正式版本,並公佈了 Go+ 發展路線圖。

Go+ 於 2020 年由許式偉面向全球推出,定位是一門為資料科學而生的程式語言。在經歷了一年多的開發後,終於釋出了 1.0 正式版本。

,它是一門適用於工程、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 生態的引入與相容。

展開閱讀全文