[golang]写了一个可以用 go 来写脚本的工具:gosl
转自:https://golangtc.com/t/53cca103320b52060a000030
写了一个可以用 go 来写脚本的工具:gosl
代码和使用说明可以看这里: 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")))
- Go Search is now operating with gosl. So you can find some good examples at: https://github.com/daviddengcn/gcse/tree/master/scripts
- go-diff also provides scripts in gosl: https://github.com/daviddengcn/go-diff/tree/master/scripts
Installation and Usage
Download and install the package
go get github.com/daviddengcn/gosl
go install github.com/daviddengcn/gosl
(Optional) Link to /bin
sudo ln -s $GOPATH/bin/gosl /bin/gosl
If you don't want to do this, the interpreter line can be like this, assuming $GOPATH/bin is in your $PATH:
#!/usr/bin/env gosl
Run a script
If a script starts with the bash interpreter line: #!/bin/gosl. You can run it like this
chmod a+x example.gs
./example.gs [params...]
Or you can explictly call gosl to run it:
gosl example.gs [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 | Example |
|---|---|---|
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)
[golang]写了一个可以用 go 来写脚本的工具:gosl的更多相关文章
- 我最近用Python写了一个算法,不需要写任何规则就能自动识别一个网页的内容
我最近用Python写了一个算法,不需要写任何规则就能自动识别一个网页的内容,目前测试了300多个新闻网站的新闻页,都能准确识别
- 写了一个常规性生成merge 的小脚本
现在使用数据库来写存储过程,动不动参数就会用到xml ,当然罗,优势也很明显,参数相对固定,而且灵活,如果要修改或者什么的,中间接口层也不需要做变化,只需要修改封装的存储过程以及程序传参就ok了. 随 ...
- 写了一个hiero中添加自定义Token的脚本
Hiero自带Token往往不够用,shotname中自带版本号的情况下要升级版本会很麻烦,比如Shot_0001_v001这样一个序列名,要升级为Shot_0001_v002就必须把_v001之前的 ...
- 我写了一个简单的JSON序列化和反序列化的工具
背景 互联网上有许多可用的Json序列化和反序列化的工具,例如fastjson,jackson,Gson等等,那么,我为什么还要自己写一个? 项目不方便依赖其他第三方库.比如有时候我们编写SDK,考虑 ...
- 用python写的一个自动卸载python包的脚本
import osplist=os.popen("pip list") # 执行windows cmd命令,获取所有包package列表,并获取返回结果到plist#跳过第1,2行 ...
- 搞了我一下午竟然是web.config少写了一个点
Safari手机版居然有个这么愚蠢的bug,浪费了我整个下午,使尽浑身解数,国内国外网站搜索解决方案,每一行代码读了又想想了又读如此不知道多少遍,想破脑袋也想不通到底哪里出了问题,结果竟然是web.c ...
- 用C3中的animation和transform写的一个模仿加载的时动画效果
用用C3中的animation和transform写的一个模仿加载的时动画效果! 不多说直接上代码; html标签部分 <div class="wrap"> <h ...
- c# .Net :Excel NPOI导入导出操作教程之List集合的数据写到一个Excel文件并导出
将List集合的数据写到一个Excel文件并导出示例: using NPOI.HSSF.UserModel;using NPOI.SS.UserModel;using System;using Sys ...
- 自己写的一个SqlHelper,感觉使用起来挺方便的
自己写的一个SqlHelper,感觉使用起来挺方便的 using System; using System.Data; using System.Collections.Generic; using ...
随机推荐
- 在 WPF 程序中应用 Windows 10 真?亚克力效果
原文:在 WPF 程序中应用 Windows 10 真?亚克力效果 从 Windows 10 (1803) 开始,Win32 应用也可以有 API 来实现原生的亚克力效果了.不过相比于 UWP 来说, ...
- docker 执行 docker system prune 导致Azure Devops build镜像失败
运行docker的centos上, 只分配了16G的空间, 装了个mysql, 还有个rancher, 就只剩下2G的空间了, Azure Devops build镜像就出错了, 显示存储空间不足, ...
- Wireless support
Wireless support 参考: https://www.rhyous.com/2010/12/03/freebsd-wireless-configuring-a-wireless-inter ...
- Ubuntu 18.04 LTS版本 谷歌拼音输入法安装
为何安装? 自带IBUS框架对中文支持不稳定 采用对中文支持稳定的fcitx框架 如何安装? 步骤如下: 卸载自带IBUS框架 命令:sudo remove ibus 安装fcitx框架 ...
- Jenkins 构建方式有几种
jenkins三种部署方式: 一.jenkins触发式构建:用于开发环境部署,开发人员push代码或者合并代码到gitlab项目的master分支,jenkins就部署代码到对应服务器. 二.jenk ...
- vue和react之间的区别
1.Vue和React之间的区别 相同点: Vue和其他框架一样,都有组件开发和虚拟dom 都支持props进行父子组件之间的数据通信 都支持数据驱动视图,不直接操作真实dom 都支持服务器端的 渲染 ...
- Oracle 操作数据库(增删改语句)
对数据库的操作除了查询,还包括插入.更新和删除等数据操作.后3种数据操作使用的 SQL 语言也称为数据操纵语言(DML). 一.插入数据(insert 语句) 插入数据就是将数据记录添加到已经存在的数 ...
- iOS pushViewController 和 presentViewController的区别 详解
pushViewController 导航控制器入栈的方式切换页面presentViewController 模态切换的方式切换页面 1:用 UINavigationController 的时候用 p ...
- 设置yum自动更新
[wang@brady ~]$ sudo yum -y install cronie yum-cron sudo vim /etc/yum/yum-cron.conf apply_updates = ...
- 腿部——哑铃&杠铃