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

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

MethodDescriptionExample
SConvert anything to a stringS(1234) == "123"
IConvert anything to an intI("1234") == 1234
BashEvalSimilar to bash backtick substitution.lsstr := BashEval("ls -l")
ExecExecute an command with argumentserr, code := Exec("rm", "-rf" "tmp")
BashExecute a bash lineerr, code := Bash("rm -rf tmp")
ScriptDirReturns the directory of the scriptfile := ScriptDir() + "/" + fn

More functions are defined in package daviddengcn/gosl/builtin/ (godoc)

### 回答1: package mainimport ( "fmt" "net/http" )func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello World") }) http.ListenAndServe(":8080", nil) } ### 回答2: 当使用Go语言编一个简单的HTTP服务器时,我们需要使用`net/http`包。下面是一个例子,展示了如何使用Go语言创建HTTP服务器并提供一个简单的"Hello, World!"页面: ```go package main import ( "net/http" ) func main() { http.HandleFunc("/", helloHandler) // 将根路径"/"绑定到helloHandler函数 http.ListenAndServe(":8080", nil) // 在本地监听8080端口并开始接收HTTP请求 } func helloHandler(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello, World!")) // 返回"Hello, World!"作为响应 } ``` 在上述代码中,我们定义了一个`helloHandler`函数,该函数会处理来自根路径的HTTP请求,并向客户端(通过`w http.ResponseWriter`)返回一个包含"Hello, World!"的响应。`http.HandleFunc`函数用于将根路径"/"绑定到`helloHandler`函数。 然后,我们使用`http.ListenAndServe`函数来监听端口8080,并启动HTTP服务器,以便开始接收来自客户端的请求。这可以通过访问http://localhost:8080/来测试。会返回"Hello, World!"的响应。 此代码片段只是一个简单的示例,你可以在这个基础上进行更多的HTTP路由和请求处理的开发。 ### 回答3: package main import ( "fmt" "net/http" ) func helloHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, World!") } func main() { http.HandleFunc("/", helloHandler) err := http.ListenAndServe(":8080", nil) if err != nil { fmt.Println("Error starting HTTP server:", err) } } 以上是一个使用Golang实现的简单的HTTP服务代码。 在main函数中,我们调用了http包中的HandleFunc方法,将根路径"/"与一个名为helloHandler的处理函数关联起来。这个处理函数接收两个参数,一个是http.ResponseWriter,用于构建HTTP响应,另一个是*http.Request,用于获取HTTP请求的信息。 helloHandler函数中使用了fmt包中的Fprintf方法,将字符串"Hello, World!"入到http.ResponseWriter中,作为HTTP响应返回给客户端。 最后,我们调用了http包中的ListenAndServe方法,指定监听的端口号为8080,并将nil作为第二个参数传递,该参数表示使用默认的路由器。如果启动HTTP服务失败,将打印错误信息到控制台。 通过运行以上代码,可以在本地启动一个简单的HTTP服务器,当访问http://localhost:8080时,服务器将返回"Hello, World!"字符串。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值