golpal

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2016 License: MIT Imports: 9 Imported by: 0

README

Golpal logo

Golpal

Easy to use Golang Eval Library

Introduction

Golpal is simple library to allow developer to do eval operation on golang source codes. Technically Golang doesn't provide API to do some eval, so we use temporary file to achieve that.

Build Status

Installation

Stable version: v1.0.0

Just go get the lib is enough

go get -u github.com/novalagung/golpal

Run test

cd $GOPATH/src/github.com/novalagung/golpal
go test *.go -v

Example

Simple Example
package main

import "github.com/novalagung/golpal"
import "fmt"

func main() {
	cmdString := `3 + 2`
	output, err := golpal.New().ExecuteSimple(cmdString)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("result", "=>", output)
}

For one line statement using ExecuteSimple(), return keyword is optional

Another Example
cmdString := `
	number := 3
	if number == 2 {
		return "wrong"
	} else {
		return "right"
	}
`

output, err := golpal.New().ExecuteSimple(cmdString)
if err != nil {
	fmt.Println(err)
}
fmt.Println("result", "=>", output)

For multiline statement using ExecuteSimple(), return must be defined

Example which use strings and runtime
cmdString := `
	osName := runtime.GOOS
	arr := []string{"my", "operation system", "is", osName}
	return strings.Join(arr, ", ")
`

output, err := golpal.New().AddLibs("strings", "runtime").ExecuteSimple(cmdString)
if err != nil {
	fmt.Println(err)
}
fmt.Println("result", "=>", output)
Example Not Simple (again, NOT SIMPLE, by using Execute() func)
cmdString := `
	func calculate(values ...int) int {
		total := 0
		for _, each := range values {
			total = total + each
		}
		return total
	}

	func main() {
		res := calculate(1, 2, 3, 4, 2, 3, 1)
		fmt.Printf("total : %d", res)
	}
`

output, err := golpal.New().Execute(cmdString)
if err != nil {
	fmt.Println(err)
}
fmt.Println("result", "=>", output)
Example Execute Raw (use ExecuteRaw() func)
cmdString := `
	package main

	import "fmt"

	func main() {
		fmt.Print("hello")
	}
`

output, err := golpal.New().ExecuteRaw(cmdString)
if err != nil {
	fmt.Println(err)
}
fmt.Println("result", "=>", output)
More Example

For more examples please take a look at the golpal_test.go file.

API Reference

Func of golpal

There are only one func available, golpal.New() which return object that type is *golpal.Golpal

Func Usage
golpal.New() instantiate new *golpal.Golpal object
Properties of *golpal.Golpal
Property Type Usage
.WillDeleteTemporaryFile bool Determine if temporary path will be deleted or not after executing the source codes (default is true)
.TemporaryFolderName string Name of temporary folder used to store all *.go temp files (default is .temp for *nix / *drwin, and temp for w*ndows)
Methods of *golpal.Golpal
Method Usage
.AddLibs(libs ...string) Add other libraries, by default only fmt is included
.ExecuteSimple(cmdString string) Run golang source codes. The code will be placed inside virtual main() func. This function doesn't allow fmt.Print*(). Also for multiline statement, return must be defined
.Execute(cmdString string) Run golang source codes which contains main() func
.ExecuteRaw(cmdString string) Run complete golang source code
.DeleteTemporaryPath() Force delete temporary path which used to do the exec process

Contribution

Feel free to contribute

fork -> commit -> push -> pull request

License

MIT License

Author

Noval Agung Prayogo - http://novalagung.com/

Documentation

Index

Constants

View Source
const (
	Version = "v1.0.0"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Golpal

type Golpal struct {
	WillDeleteTemporaryFile bool
	TemporaryFolderName     string
	// contains filtered or unexported fields
}

func New

func New() *Golpal

func (*Golpal) AddLibs

func (g *Golpal) AddLibs(libs ...string) *Golpal

func (*Golpal) DeleteTemporaryPath

func (g *Golpal) DeleteTemporaryPath()

func (*Golpal) Execute

func (g *Golpal) Execute(cmdString string) (string, error)

func (*Golpal) ExecuteRaw

func (g *Golpal) ExecuteRaw(cmdString string) (string, error)

func (*Golpal) ExecuteSimple

func (g *Golpal) ExecuteSimple(cmdString string) (string, error)

Jump to

Keyboard shortcuts

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