代码和使用说明可以看这里: http://github.com/daviddengcn/gosl
Go Search 已经完全用 gosl 来启动了。
相比 bash/Python 写脚本的好处:
- 纯 Go 语言,没有新的学习成本
- 引入预先导入的 package 和预定义的内置函数,方便脚本书写
- 可以无缝的和 Go 项目衔接,例如可以直接读取数据和配置。
- 和 Go 有相同的执行效率,大大快过 Python
这里贴一个简单的例子:
#!/bin/gosl APPS := []string { "tocrawl", "crawler", "mergedocs", "indexer", } for { for _, app := range APPS { Printf("Running %s...\n", app) Bash(app) } }gosl
This is an application that can make you write script with the Go language.
It is NOT an interpreter but the pure Go. The preprocessor tranforms the script into a Go program, instantly compiles and runs. So it is almost same as the standard Go with the same efficiency.
Benefit
- Pure Go language. No need to learn a new script language.
- Pre-imported packages and pre-defined functions make it easy to code.
- Seamless integration with the Go project. E.g. can easily load configuration or data file from the Go project.
- Running efficiency same as Go, much faster than Python.
Example
- Simple
#!/bin/gosl import "encoding/json" toJson := func(lines []string) string { res, _ := json.Marshal(struct { Lines []string `json:"lines"` }{ Lines: lines, }) return string(res) } files := BashEval("ls -l %s", "/tmp/") Println(toJson(Split(files, "\n")))
Installation and Usage
Download and install the package
go get github.com/daviddengcn/gosl go install github.com/daviddengcn/gosl
/bin
sudo ln -s $GOPATH/bin/gosl /bin/gosl
$GOPATH/bin$PATH
#!/usr/bin/env gosl
Run a script
#!/bin/gosl
chmod a+x example.gs ./example.gs [params...]
gosl
gosl example.gs [params...]
Pre-imported Packages
.
fmtosstringsstrconvmathtimegithub.com/daviddengcn/gosl/builtin
builtin
SstringS(1234) == "123"IintI("1234") == 1234BashEvallsstr := BashEval("ls -l")Execerr, code := Exec("rm", "-rf" "tmp")Basherr, code := Bash("rm -rf tmp")ScriptDirfile := ScriptDir() + "/" + fn
More functions are defined in package daviddengcn/gosl/builtin/ (godoc)