前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[golang]写了一个可以用 go 来写脚本的工具:gosl

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

作者头像
landv
发布2019-09-30 10:27:48
2K0
发布2019-09-30 10:27:48
举报
文章被收录于专栏:landv

转自: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

这里贴一个简单的例子:

代码语言:javascript
复制
#!/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
代码语言:javascript
复制
#!/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)

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-09-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 转自:https://golangtc.com/t/53cca103320b52060a000030
  • 写了一个可以用 go 来写脚本的工具:gosl
  • gosl 
    • Benefit
      • Example
        • Installation and Usage
          • Pre-imported Packages
            • Frequently Used builtin Functions
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档