/goplus

GoPlus - The Go+ language for data science

Primary LanguageGoApache License 2.0Apache-2.0

GoPlus - The Go+ language for data science

LICENSE Build Status Go Report Card GitHub release Coverage Status GoDoc

Summary about Go+

What are mainly impressions about Go+?

  • A static typed language.
  • Fully compatible with the Go language.
  • Script-like style, and more readable code for data science than Go.

For example, the following is a legal Go+ source code:

a := [1, 2, 3.4]
println(a)

How do we do this in the Go language?

package main

func main() {
    a := []float64{1, 2, 3.4}
    println(a)
}

Of course, we don't only do less-typing things.

For example, we support list comprehension, which make data processing easier.

a := [1, 3, 5, 7, 11]
b := [x*x for x <- a, x > 3]
println(b) // output: [25 49 121]

mapData := {"Hi": 1, "Hello": 2, "Go+": 3}
reversedMap := {v: k for k, v <- mapData}
println(reversedMap) // output: map[1:Hi 2:Hello 3:Go+]

We will keep Go+ simple. This is why we call it Go+, not Go++.

Less is exponentially more.

It's for Go, and it's also for Go+.

Compatibility with Go

All Go features (not including cgo) will be supported.

All Go packages (even these packages use cgo) can be imported by Go+.

import (
    "fmt"
    "strings"
)

x := strings.NewReplacer("?", "!").Replace("hello, world???")
fmt.Println("x:", x)

Be interested in how it works? See Dive into Go+.

Also, all Go+ packages can be converted into Go packages, and then be imported by Go.

First, let's make a directory named tutorial/14-Using-goplus-in-Go.

Then write a Go+ package named foo in it:

package foo

func ReverseMap(m map[string]int) map[int]string {
    return {v: k for k, v <- m}
}

Then use it in a Go package:

package main

import (
	"fmt"

	"github.com/qiniu/goplus/tutorial/14-Using-goplus-in-Go/foo"
)

func main() {
	rmap := foo.ReverseMap(map[string]int{"Hi": 1, "Hello": 2})
	fmt.Println(rmap)
}

How to compile this exmaple?

gop go tutorial/ # Convert all Go+ packages in tutorial/ into Go packages
go install ./...

Or:

gop install ./... # Convert Go+ packages and go install ./...

Go tutorial/14-Using-goplus-in-Go to get the source code.

Tutorials

Go+ features

Bytecode vs. Go code

Go+ suppports bytecode backend or generating Go code.

When we use gop go or gop install command, it generates Go code to covert Go+ package into Go packages.

When we use gop run command, it doesn't call go run command. It generates bytecode to execute.

Commands

gop go <gopSrcDir> # Convert all Go+ packages under <gopSrcDir> into Go packages, recursively
gop run <gopSrcDir> # Running <gopSrcDir> as a Go+ main package
gop run <gopSrcFile> # Running <gopSrcFile> as a Go+ script
gop install ./... # Convert all Go+ packages under ./ and go install ./...

The gop command isn't provided currently (in alpha stage). Instead, we provide the following commands:

qrun <gopSrcDir> # gop run <gopSrcDir>
qrun -asm <gopSrcDir> # generates `asm` code of Go+ bytecode backend
qrun -quiet <gopSrcDir> # don't generate any compiling stage log
qgo <gopSrcDir> # gop go <gopSrcDir>
qgo -test <gopSrcDir>

Note:

  • qgo -test <gopSrcDir> converts Go+ packages into Go packages, and for every package, it call go run <gopPkgDir>/gop_autogen.go and qrun -quiet <gopPkgDir> to compare their outputs. If their outputs aren't equal, the test case fails.

Map literal

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{}

Slice literal

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{}

List/Map comprehension

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}

For loop

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

Go features

All Go features (not including cgo) will be supported.