script库是什么

script is a Go library for doing the kind of tasks that shell scripts are good at: reading files, executing subprocesses, counting lines, matching strings, and so on.
  • Github:https://github.com/bitfield/script

    安装

    # 初始化模块(如果尚未初始化)
    go mod init mymodule
    
    # 安装脚本包
    go install github.com/bitfield/script@latest

    示例代码

    package main
    
    import (
      "fmt"
      "github.com/bitfield/script"
    )
    
    func main() {
      // 使用 script 包创建一个简单的脚本
      output, err := script.Exec("echo Hello, World!").String()
      
      // 其他demo
      //  执行Ping
      // script.Args().ExecForEach("ping -c 1 {{.}}").Stdout()
      if err != nil {
          fmt.Println("Error:", err)
          return
      }
    
      fmt.Println(output)
    }

    其他食用方法,可以查阅github中的README.md

标签: golang, go, shell, script

添加新评论