转自:https://golangtc.com/t/53cca103320b52060a000030

写了一个可以用 go 来写脚本的工具:gosl

代码和使用说明可以看这里: http://github.com/daviddengcn/gosl

Go Search 已经完全用 gosl 来启动了。

相比 bash/Python 写脚本的好处:

  1. 纯 Go 语言,没有新的学习成本
  2. 引入预先导入的 package 和预定义的内置函数,方便脚本书写
  3. 可以无缝的和 Go 项目衔接,例如可以直接读取数据和配置。
  4. 和 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

  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 configuration or data file from the Go project.
  4. 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

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

fmtosstringsstrconvmathtime, 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的更多相关文章

  1. 我最近用Python写了一个算法,不需要写任何规则就能自动识别一个网页的内容

    我最近用Python写了一个算法,不需要写任何规则就能自动识别一个网页的内容,目前测试了300多个新闻网站的新闻页,都能准确识别

  2. 写了一个常规性生成merge 的小脚本

    现在使用数据库来写存储过程,动不动参数就会用到xml ,当然罗,优势也很明显,参数相对固定,而且灵活,如果要修改或者什么的,中间接口层也不需要做变化,只需要修改封装的存储过程以及程序传参就ok了. 随 ...

  3. 写了一个hiero中添加自定义Token的脚本

    Hiero自带Token往往不够用,shotname中自带版本号的情况下要升级版本会很麻烦,比如Shot_0001_v001这样一个序列名,要升级为Shot_0001_v002就必须把_v001之前的 ...

  4. 我写了一个简单的JSON序列化和反序列化的工具

    背景 互联网上有许多可用的Json序列化和反序列化的工具,例如fastjson,jackson,Gson等等,那么,我为什么还要自己写一个? 项目不方便依赖其他第三方库.比如有时候我们编写SDK,考虑 ...

  5. 用python写的一个自动卸载python包的脚本

    import osplist=os.popen("pip list") # 执行windows cmd命令,获取所有包package列表,并获取返回结果到plist#跳过第1,2行 ...

  6. 搞了我一下午竟然是web.config少写了一个点

    Safari手机版居然有个这么愚蠢的bug,浪费了我整个下午,使尽浑身解数,国内国外网站搜索解决方案,每一行代码读了又想想了又读如此不知道多少遍,想破脑袋也想不通到底哪里出了问题,结果竟然是web.c ...

  7. 用C3中的animation和transform写的一个模仿加载的时动画效果

    用用C3中的animation和transform写的一个模仿加载的时动画效果! 不多说直接上代码; html标签部分 <div class="wrap"> <h ...

  8. c# .Net :Excel NPOI导入导出操作教程之List集合的数据写到一个Excel文件并导出

    将List集合的数据写到一个Excel文件并导出示例: using NPOI.HSSF.UserModel;using NPOI.SS.UserModel;using System;using Sys ...

  9. 自己写的一个SqlHelper,感觉使用起来挺方便的

    自己写的一个SqlHelper,感觉使用起来挺方便的 using System; using System.Data; using System.Collections.Generic; using ...

随机推荐

  1. java mybatis

    mybatis简单使用记录一下 mybatis官网:http://www.mybatis.org/mybatis-3/ 参考博客:https://blog.csdn.net/iku5200/artic ...

  2. MyBatis 中#和$符号的区别

    #相当于对数据 加上 双引号,$相当于直接显示数据 1. #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号.如:order by #user_id#,如果传入的值是111,那么解析成sq ...

  3. Web应用和Web框架

    一.Web应用 二.Web框架 三.wsgiref模块 一.Web应用 1.什么是Web应用? Web应用程序是一种可以通过Web访问的应用程序,特点是用户很容易访问,只需要有浏览器即可,不需要安装其 ...

  4. 【转载】C#中string类使用Replace方法来替换字符串

    在C#的字符串操作过程中,有时候需要替换字符串中的某个子字符串,此时就可以使用到字符串类自带的Replace方法来实现,Replace方法将查找到所有符合被替换的子字符串,然后将之全部替换为目标字符串 ...

  5. DDL 操作数据库

    DDL 操作数据库:常用的操作 CRUD 一.C(create)创建 1.创建数据库 create database 数据库名称; 2.创建数据库,判断是否存在,再创建(如果存在,就不再创建) cre ...

  6. Android里的Dalvik、ART、JIT、AOT有什么关系?

    JIT,Just-in-time,即时编译,边运行边编译: AOT,Ahead Of Time,提前编译,指运行前编译. 区别 这两种编译方式的主要区别在于是否在“运行时”进行编译 优劣JIT优点: ...

  7. 一个时间O(n)的洗牌算法

    //一种O(n)的洗牌算法 vector<int> randNUms(vector<int> &nums, int m) { int len = nums.size() ...

  8. MySQL Index--CREATE INDEX在各版本的优化

    FIC(Fast index creation)特性在MySQL 5.5版本中引入FIC(Fast index creation)特性,创建索引时无需再拷贝整表数据,以提升索引的创建速度. FCI 操 ...

  9. 一:MySQL系列之基本介绍(一)

    本篇主要介绍关于MySQL数据的基本知识,包括数据存储的变化,什么是MySQL以及其有什么优点.以及什么是RDBMS概念性知识等,以及关于MySQL语句的SOL的基本用法: 一.数据库 数据库,顾名思 ...

  10. websocket之简易聊天室

    一,带昵称的群聊 #!/usr/bin/env python # -*- coding:utf8 -*- import json from flask import Flask, request, r ...