GithubHelp home page GithubHelp logo

gosl's Introduction

gosl GoSearch

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.

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)

License

Apache License V2

gosl's People

Contributors

daviddengcn avatar

Stargazers

Everton Ribeiro avatar daniel lee avatar  avatar  avatar  avatar Benson Wodehous avatar bearcat-panda avatar  avatar yanglei avatar  avatar  avatar  avatar  avatar  avatar lpp avatar  avatar will avatar landv avatar Robert Muller avatar soyum2222 avatar  avatar  avatar Andrew Missel avatar  avatar idk avatar Mikal avatar Heyward Fann avatar Eric Wong avatar  avatar euclid avatar Fei Yang avatar Anner van Hardenbroek avatar Terry avatar  avatar Zach Norman avatar Eric Myhre avatar PJ Eby avatar henryscala avatar w4v3 avatar 不在乎y avatar Paulo Pereira avatar Janez Troha avatar Rogerio Marques avatar Maxime Lasserre avatar Orkan Alat avatar Shenhuan Chan avatar  avatar SuperWalle avatar vincent.plet avatar haitgo avatar Dan Cadden avatar Kuo-tung Kao avatar  avatar cjuexuan avatar Jetlum Ajeti avatar Harry Lawrence avatar Adam Stokes avatar Angus H. avatar daemtri avatar tabu avatar HongZhen Peng avatar Mauro Junior avatar ACoder avatar lwPan avatar charles.deng avatar nicedemo avatar  avatar sheppard avatar chenhao avatar  avatar xing avatar codelife avatar Denis Denisov avatar wangkechun avatar badb0y avatar ChenYe avatar Leon Lee avatar HaoLin avatar Bob Liu avatar BoYang avatar Isagani Mendoza avatar shouda avatar mind avatar  avatar Shawn avatar Simon Waldherr avatar  avatar Oleg Lebedev avatar wangtuanjie avatar Jonsen Yang avatar Lunny Xiao avatar Ben Aldrich avatar 壹楽 avatar  avatar cinience avatar Yuji Takaesu avatar Leiguang Hao avatar Henson Lu avatar  avatar Anton Bulygin avatar

Watchers

 avatar 吕志浩 avatar James Cloos avatar Nikhil avatar  avatar  avatar Zach Norman avatar LiAo avatar  avatar  avatar  avatar

gosl's Issues

feature request: caching

It might make sense to maintain some small cache of recently run scripts. You could do this by generating a hash of the file contents and using that when you generate the file name to use, if the file already exists, execute it and leave the binary, if not, generate it.

You'd have to decide how many recent files to keep and make sure old entries are cleaned up so that binaries don't end up taking up a lot of space, but you could touch the files and purge least recently used files after some threshold size is reached.

This would be useful for scripts that are run very frequently I think, which is probably a common use case.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.