运行 使用 golang 的命令行?
Run a command line using golang?
我只是在玩 golang。我很好奇我如何 运行 go 中的 gulpfile 任务?
Gulp 来自终端的 运行 典型任务:
gulp serv.dev
我怎么能运行这行来自golang的简单代码:
package main
import (
"net/http"
"github.com/julienschmidt/httprouter"
"fmt"
)
func main() {
//what do I put here to open terminal in background and run `gulp serv.dev`
}
看看exec。对于您的用例:
package main
import (
"net/http"
"github.com/julienschmidt/httprouter"
"fmt"
"os/exec"
"log"
)
func main() {
out, err := exec.Command("gulp", "serv.dev").Output()
if err != nil {
log.Fatal(err)
}
fmt.Printf("The date is %s\n", out)
}
您要找的是exec.Command
您将非常希望生成一个进程来 运行 您的 gulp
任务。
可以这样做:
package main
import (
"os/exec"
)
func main() {
cmd := exec.Command("gulp", "serv.dev")
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
}
您很可能需要 exec package
cmd := exec.Command("gulp", "serv.dev")
err := cmd.Run()
查看 exec.Command 中的示例。他们解释了如何传递参数和读取输出。
更通用,输出更好。
使用 exec.Command 以及 缓冲区 记录输出并仅在有用时显示。
您甚至可以通过使用可变参数,即任意数量元素的参数,使函数与任何命令一起工作。
适当地标记未处理的错误,因此如果命令失败,您会被告知是哪个错误以及原因。
最后请注意,Go 虽然富有表现力,但却是一门相当原始语言。它无缘无故地握着你的手。你将不得不自己编写大量程序。
示例代码:
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"runtime"
"strings"
)
func main() {
runCommand(currentFunction(), "ping", "-c1", "google.commm")
}
func commandErrorMessage(stderr bytes.Buffer, program string) string {
message := string(stderr.Bytes())
if len(message) == 0 {
message = "the command doesn't exist: " + program + "\n"
}
return message
}
func currentFunction() string {
counter, _, _, success := runtime.Caller(1)
if !success {
println("functionName: runtime.Caller: failed")
os.Exit(1)
}
return runtime.FuncForPC(counter).Name()
}
func printCommandError(stderr bytes.Buffer, callerFunc string, program string, args ...string) {
printCommandErrorUbication(callerFunc, program, args...)
fmt.Fprintf(os.Stderr, "%s", commandErrorMessage(stderr, program))
}
func printCommandErrorUbication(callerFunc string, program string, args ...string) {
format := "error at: %s: %s %s\n"
argsJoined := strings.Join(args, " ")
fmt.Fprintf(os.Stderr, format, callerFunc, program, argsJoined)
}
func runCommand(callerFunc string, program string, args ...string) {
command := exec.Command(program, args...)
var stderr bytes.Buffer
command.Stderr = &stderr
fail := command.Run()
if fail != nil {
printCommandError(stderr, callerFunc, program, args...)
os.Exit(1)
}
}
示例运行:
$ go run test.go
error at: main.main: ping -c1 google.commm
ping: google.commm: Name or service not known
exit status 1
我只是在玩 golang。我很好奇我如何 运行 go 中的 gulpfile 任务?
Gulp 来自终端的 运行 典型任务:
gulp serv.dev
我怎么能运行这行来自golang的简单代码:
package main
import (
"net/http"
"github.com/julienschmidt/httprouter"
"fmt"
)
func main() {
//what do I put here to open terminal in background and run `gulp serv.dev`
}
看看exec。对于您的用例:
package main
import (
"net/http"
"github.com/julienschmidt/httprouter"
"fmt"
"os/exec"
"log"
)
func main() {
out, err := exec.Command("gulp", "serv.dev").Output()
if err != nil {
log.Fatal(err)
}
fmt.Printf("The date is %s\n", out)
}
您要找的是exec.Command
您将非常希望生成一个进程来 运行 您的 gulp
任务。
可以这样做:
package main
import (
"os/exec"
)
func main() {
cmd := exec.Command("gulp", "serv.dev")
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
}
您很可能需要 exec package
cmd := exec.Command("gulp", "serv.dev")
err := cmd.Run()
查看 exec.Command 中的示例。他们解释了如何传递参数和读取输出。
更通用,输出更好。
使用 exec.Command 以及 缓冲区 记录输出并仅在有用时显示。
您甚至可以通过使用可变参数,即任意数量元素的参数,使函数与任何命令一起工作。
适当地标记未处理的错误,因此如果命令失败,您会被告知是哪个错误以及原因。
最后请注意,Go 虽然富有表现力,但却是一门相当原始语言。它无缘无故地握着你的手。你将不得不自己编写大量程序。
示例代码:
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"runtime"
"strings"
)
func main() {
runCommand(currentFunction(), "ping", "-c1", "google.commm")
}
func commandErrorMessage(stderr bytes.Buffer, program string) string {
message := string(stderr.Bytes())
if len(message) == 0 {
message = "the command doesn't exist: " + program + "\n"
}
return message
}
func currentFunction() string {
counter, _, _, success := runtime.Caller(1)
if !success {
println("functionName: runtime.Caller: failed")
os.Exit(1)
}
return runtime.FuncForPC(counter).Name()
}
func printCommandError(stderr bytes.Buffer, callerFunc string, program string, args ...string) {
printCommandErrorUbication(callerFunc, program, args...)
fmt.Fprintf(os.Stderr, "%s", commandErrorMessage(stderr, program))
}
func printCommandErrorUbication(callerFunc string, program string, args ...string) {
format := "error at: %s: %s %s\n"
argsJoined := strings.Join(args, " ")
fmt.Fprintf(os.Stderr, format, callerFunc, program, argsJoined)
}
func runCommand(callerFunc string, program string, args ...string) {
command := exec.Command(program, args...)
var stderr bytes.Buffer
command.Stderr = &stderr
fail := command.Run()
if fail != nil {
printCommandError(stderr, callerFunc, program, args...)
os.Exit(1)
}
}
示例运行:
$ go run test.go
error at: main.main: ping -c1 google.commm
ping: google.commm: Name or service not known
exit status 1