package main
import (
"bytes"
"flag"
"fmt"
"github.com/golang/glog"
"os/exec"
)
func main() {
var cmd *exec.Cmd
//初始化命令行参数
flag.Parse()
//退出时调用,确保日志写入磁盘文件中
defer glog.Flush()
cmd = exec.Command("/bin/sh", "-c", "ps -e | grep send_test_data")
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
process_str := out.String()
fmt.Print(process_str)
if err != nil {
fmt.Println(fmt.Sprint(err) + ": @@@@" + stderr.String())
}
}
以上代码的文件名名称为 send_test_data.go
运行返回结果:exit status 1: @@@@
package main
import (
"bytes"
"flag"
"fmt"
"github.com/golang/glog"
"os/exec"
)
func main() {
var cmd *exec.Cmd
//初始化命令行参数
flag.Parse()
//退出时调用,确保日志写入磁盘文件中
defer glog.Flush()
cmd = exec.Command("/bin/sh", "-c", "ps -e | grep send_data")
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
process_str := out.String()
fmt.Print(process_str)
if err != nil {
fmt.Println(fmt.Sprint(err) + ": @@@@" + stderr.String())
}
}
以上代码的文件名名称为 send_data.go
运行返回结果:34211 pts/1 00:00:00 send_data
https://golang.google.cn/doc/effective_go#mixed-caps
官方文档:
MixedCaps
Finally, the convention in Go is to use MixedCaps
or mixedCaps
rather than underscores to write multiword names.
结论:
1、尽量不要使用下划线来分隔文件名,即使使用,也不要超过1个下划线;
ERROR:go run: cannot run *_test.go files
比如执行:go run aaa_test.go报如上错误。
*_test.go是golang特有的约定,为测试文件。
可以深入了解一下 go test相关。