gop

package module
v0.7.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 20, 2020 License: Apache-2.0 Imports: 1 Imported by: 242

README

GoPlus - The Go+ language for data science

Build Status Go Report Card GitHub release Coverage Status REPL Playground VSCode 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 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 makes 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 will be supported (including partially support cgo, see below).

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 Import Go packages in Go+ programs.

Also, all Go+ packages can be converted into Go packages and imported in Go programs.

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/goplus/gop/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 example?

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.

Playground/REPL

Go+ REPL based on GopherJS/WASM:

Go+ Playground based on Docker:

Go+ Playground based on GopherJS:

Tutorials

See https://github.com/goplus/gop/tree/master/tutorial

How to build

git clone git@github.com:goplus/gop.git
cd gop
go install -v ./...

Go+ features

Bytecode vs. Go code

Go+ supports bytecode backend and Go code generation.

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.

In bytecode mode, Go+ doesn't support cgo. However, in Go-code-generation mode, Go+ fully supports cgo.

Commands
gop run         # Run a Go+ program
gop repl        # Run Go+ in REPL/Console mode
gop go [-test]  # Convert Go+ packages into Go packages. If -test specified, it tests related packages.
gop fmt         # Format Go+ packages
gop export      # Export Go packages for Go+ programs

See https://github.com/goplus/gop/wiki/Commands for details.

Note:

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

We introduce the rational number as native Go+ types. We use suffix r to denote rational literals. For example, (1r << 200) means a big int whose value is equal to 2200. And 4/5r means the rational constant 4/5.

a := 1r << 65   // bigint, large than int64
b := 4/5r       // bigrat
c := b - 1/3r + 3 * 1/2r
println(a, b, c)
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
}
Error handling

We reinvent the error handling specification in Go+. We call them ErrWrap expressions:

expr! // panic if err
expr? // return if err
expr?:defval // use defval if err

How to use them? Here is an example:

import (
	"strconv"
)

func add(x, y string) (int, error) {
	return strconv.Atoi(x)? + strconv.Atoi(y)?, nil
}

func addSafe(x, y string) int {
	return strconv.Atoi(x)?:0 + strconv.Atoi(y)?:0
}

println(`add("100", "23"):`, add("100", "23")!)

sum, err := add("10", "abc")
println(`add("10", "abc"):`, sum, err)

println(`addSafe("10", "abc"):`, addSafe("10", "abc"))

The output of this example is:

add("100", "23"): 123
add("10", "abc"): 0 strconv.Atoi: parsing "abc": invalid syntax

===> errors stack:
main.add("10", "abc")
	/Users/xsw/goplus/tutorial/15-ErrWrap/err_wrap.gop:6 strconv.Atoi(y)?

addSafe("10", "abc"): 10

Compared to corresponding Go code, It is clear and more readable.

And the most interesting thing is, the return error contains the full error stack. When we got an error, it is very easy to position what the root cause is.

How these ErrWrap expressions work? See Error Handling for more information.

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())

In many languages, there is a concept named 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)

In Go+, we introduce a concept named 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.

Unix shebang

You can use Go+ programs as shell scripts now. For example:

#!/usr/bin/env qrun

println("Hello, Go+")

println(1r << 129)
println(1/3r + 2/7r*2)

arr := [1, 3, 5, 7, 11, 13, 17, 19]
println(arr)
println([x*x for x <- arr, x > 3])

m := {"Hi": 1, "Go+": 2}
println(m)
println({v: k for k, v <- m})
println([k for k, _ <- m])
println([v for v <- m])

Go tutorial/20-Unix-Shebang/shebang to get the source code.

Go features

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

IDE Plugins

Contributing

The Go+ project welcomes all contributors. We appreciate your help!

Here are list of Go+ Contributors. We award an email account (XXX@goplus.org) for every contributor. And we suggest you commit code by using this email account:

git config --global user.email XXX@goplus.org

If you did this, remember to add your XXX@goplus.org email to https://github.com/settings/emails.

What does a contributor to Go+ mean? He must meet one of the following conditions:

  • At least one pull request of a full-featured implemention.
  • At least three pull requests of feature enhancements.
  • At least ten pull requests of any kind issues.

Where can you start?

  • Issues
  • Issues
  • Issues
  • Issues
  • TODOs

Documentation

Overview

Package gop provides all interfaces for implementing a Go+ package from existed Go packages.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToBools added in v0.7.2

func ToBools(args []interface{}) []bool

ToBools converts []interface{} into []bool.

func ToError

func ToError(v interface{}) error

ToError converts a value into error.

func ToStrings

func ToStrings(args []interface{}) []string

ToStrings converts []interface{} into []string.

Types

type Context

type Context = exec.Context

A Context represents the context of an executor.

type GoPackage

type GoPackage = exec.GoPackage

GoPackage represents a Go package.

func NewGoPackage

func NewGoPackage(pkgPath string) *GoPackage

NewGoPackage creates a new builtin Go Package.

Directories

Path Synopsis
ast
Package ast declares the types used to represent syntax trees for Go+ packages.
Package ast declares the types used to represent syntax trees for Go+ packages.
cl
Package cl compiles Go+ syntax trees (ast) into a backend code.
Package cl compiles Go+ syntax trees (ast) into a backend code.
cmd
gop
internal/base
Package base defines shared basic pieces of the gop command, in particular logging and the Command structure.
Package base defines shared basic pieces of the gop command, in particular logging and the Command structure.
internal/export
Package export implements the “gop export” command.
Package export implements the “gop export” command.
internal/export/srcimporter
Package srcimporter implements importing directly from source files rather than installed packages.
Package srcimporter implements importing directly from source files rather than installed packages.
internal/gengo
Package gengo implements the “gop go” command.
Package gengo implements the “gop go” command.
internal/gopfmt
Package gopfmt implements the “gop fmt” command.
Package gopfmt implements the “gop fmt” command.
internal/help
Package help implements the “go help” command.
Package help implements the “go help” command.
internal/repl
Package repl implements the “gop repl” command.
Package repl implements the “gop repl” command.
internal/run
Package run implements the “gop run” command.
Package run implements the “gop run” command.
qgo
Package constant implements Values representing untyped Go+ constants and their corresponding operations.
Package constant implements Values representing untyped Go+ constants and their corresponding operations.
exec
bytecode
Package bytecode implements a bytecode backend for the Go+ language.
Package bytecode implements a bytecode backend for the Go+ language.
golang
Package golang implements a golang backend for Go+ to generate Go code.
Package golang implements a golang backend for Go+ to generate Go code.
golang/internal/go/format
Package format implements standard formatting of Go source.
Package format implements standard formatting of Go source.
golang/internal/go/printer
Package printer implements printing of AST nodes.
Package printer implements printing of AST nodes.
Package exec defines the specification of a Go+ backend.
Package exec defines the specification of a Go+ backend.
Package format implements standard formatting of Go+ source.
Package format implements standard formatting of Go+ source.
lib
builtin
Package builtin provide Go+ builtin stuffs, including builtin constants, types and functions.
Package builtin provide Go+ builtin stuffs, including builtin constants, types and functions.
bytes
Package bytes provide Go+ "bytes" package, as "bytes" package in Go.
Package bytes provide Go+ "bytes" package, as "bytes" package in Go.
flag
Package flag provide Go+ "flag" package, as "flag" package in Go.
Package flag provide Go+ "flag" package, as "flag" package in Go.
fmt
Package fmt provide Go+ "fmt" package, as "fmt" package in Go.
Package fmt provide Go+ "fmt" package, as "fmt" package in Go.
github.com/goplus/gop/ast/gopq
Package gopq provide Go+ "github.com/goplus/gop/ast/gopq" package, as "github.com/goplus/gop/ast/gopq" package in Go.
Package gopq provide Go+ "github.com/goplus/gop/ast/gopq" package, as "github.com/goplus/gop/ast/gopq" package in Go.
github.com/goplus/gop/ast/goptest
Package goptest provide Go+ "github.com/goplus/gop/ast/goptest" package, as "github.com/goplus/gop/ast/goptest" package in Go.
Package goptest provide Go+ "github.com/goplus/gop/ast/goptest" package, as "github.com/goplus/gop/ast/goptest" package in Go.
io
Package io provide Go+ "io" package, as "io" package in Go.
Package io provide Go+ "io" package, as "io" package in Go.
io/ioutil
Package ioutil provide Go+ "io/ioutil" package, as "io/ioutil" package in Go.
Package ioutil provide Go+ "io/ioutil" package, as "io/ioutil" package in Go.
log
Package log provide Go+ "log" package, as "log" package in Go.
Package log provide Go+ "log" package, as "log" package in Go.
os
Package os provide Go+ "os" package, as "os" package in Go.
Package os provide Go+ "os" package, as "os" package in Go.
reflect
Package reflect provide Go+ "reflect" package, as "reflect" package in Go.
Package reflect provide Go+ "reflect" package, as "reflect" package in Go.
strconv
Package strconv provide Go+ "strconv" package, as "strconv" package in Go.
Package strconv provide Go+ "strconv" package, as "strconv" package in Go.
strings
Package strings provide Go+ "strings" package, as "strings" package in Go.
Package strings provide Go+ "strings" package, as "strings" package in Go.
sync
Package sync provide Go+ "sync" package, as "sync" package in Go.
Package sync provide Go+ "sync" package, as "sync" package in Go.
sync/atomic
Package atomic provide Go+ "sync/atomic" package, as "sync/atomic" package in Go.
Package atomic provide Go+ "sync/atomic" package, as "sync/atomic" package in Go.
time
Package time provide Go+ "time" package, as "time" package in Go.
Package time provide Go+ "time" package, as "time" package in Go.
mod
semver
Package semver implements comparison of semantic version strings.
Package semver implements comparison of semantic version strings.
Package parser implements a parser for Go+ source files.
Package parser implements a parser for Go+ source files.
Package printer implements printing of AST nodes.
Package printer implements printing of AST nodes.
Package scanner implements a scanner for Go+ source text.
Package scanner implements a scanner for Go+ source text.
Package token defines constants representing the lexical tokens of the Go+ programming language and basic operations on tokens (printing, predicates).
Package token defines constants representing the lexical tokens of the Go+ programming language and basic operations on tokens (printing, predicates).
tutorial
Package xtypes provides `go/types` extended utilities.
Package xtypes provides `go/types` extended utilities.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL