gosl

command module
v0.0.0-...-9d360cc Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2014 License: Apache-2.0 Imports: 11 Imported by: 0

README

gosl

This is an application that can make you write script with Go languages.

It is NOT an interpreter but the pure Go. The preprocessor tranforms the script into a Go program, instantly compile and run. So it is almost same as the standard Go with the same efficiency.

Benefit

  1. Pure Go language. No need to learn a new script language.
  2. Pre-imported packages and pre-defined functions make it easy to code.
  3. Seamless integration with the Go project. E.g. can easily load configuratio or data file from the Go project.
  4. Running efficiency same as Go, much faster than Python.

Example

#!/bin/gosl

import "encoding/json"

toJson := func(lines []string) string {
  res, _ := json.Marshal(struct {
    Lines []string `json:"lines"`
  }{
    Lines: lines,
  })
  return string(res)
}

files, _, _ := BashWithStdout("ls -l /tmp/")

Println(toJson(Split(files, "\n")))
    

Go Search is now operating with gosl. So you can find some good examples at: https://github.com/daviddengcn/gcse/tree/master/scripts

Installation and Usage

Download and install the package
go get github.com/daviddengcn/gosl
go install github.com/daviddengcn/gosl
sudo ln -s $GOPATH/bin/gosl /bin/ls
Run a script

If a script starts with the bash interpreter line: #!/bin/gosl. You can run it like this

chmod a+x example.go
./example.go [params...]

Or you can explictly call gosl to run it:

gosl example.go [params...]

Pre-imported Packages

The following packages are pre-imported with ., i.e. you can directly use the methods exported by them. No complain of the compiler if you don't use them.

fmt, os, strings, strconv, math, time, and github.com/daviddengcn/gosl/builtin

Frequently Used Builtin Functions

Method Description Examples
S Convert anything to a string S(1234) == "123"
I Convert anything to an int I("1234") == 1234
BashEval Similar to bash backtick substitution. lsstr := BashEval("ls -l")
Exec Execute an command with arguments err, code := Exec("rm", "-rf" "tmp")
Bash Execute a bash line err, code := Bash("rm -rf tmp")
ScriptDir Returns the directory of the script file := ScriptDir() + "/" + fn

More functions are defined in package daviddengcn/gosl/builtin/ (godoc)

License

Apache License V2

Documentation

Overview

This is an application that can make you write script with Go languages.

It is NOT an interpreter but the pure Go. The preprocessor tranforms the script into a Go program, instantly compile and run. So it is almost same as the standard Go with the same efficiency.

Directories

Path Synopsis
Package builtin implements some built-in functions for gosl (Go Language Script Language, github.com/daviddengcn/gosl) For use of convinience as a script language, the parameters are commonly defined as an interface{}.
Package builtin implements some built-in functions for gosl (Go Language Script Language, github.com/daviddengcn/gosl) For use of convinience as a script language, the parameters are commonly defined as an interface{}.

Jump to

Keyboard shortcuts

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