源码 os.StartProcess exec.Command StartProcess is a low-level interface. The os/exec package provides // higher-level interfaces. 启动另一进程 命令行参数 第二个 CommandLine.Parse(os.Args[1:])

Go/src/os/exec.go:96

// StartProcess starts a new process with the program, arguments and attributes
// specified by name, argv and attr. The argv slice will become os.Args in the
// new process, so it normally starts with the program name.
//
// If the calling goroutine has locked the operating system thread
// with runtime.LockOSThread and modified any inheritable OS-level
// thread state (for example, Linux or Plan 9 name spaces), the new
// process will inherit the caller's thread state.
//
// StartProcess is a low-level interface. The os/exec package provides
// higher-level interfaces.
//
// If there is an error, it will be of type *PathError.
func StartProcess(name string, argv []string, attr *ProcAttr) (*Process, error) {
testlog.Open(name)
return startProcess(name, argv, attr)
}

// Parse parses the command-line flags from os.Args[1:]. Must be called
// after all flags are defined and before flags are accessed by the program.
func Parse() {
// Ignore errors; CommandLine is set for ExitOnError.
CommandLine.Parse(os.Args[1:])
}



package main

import (
"flag"
"fmt"
"time"
)

func main() {
var TaskType string
flag.StringVar(&TaskType, "TASKTYPE", "TODO", "")
flag.Parse()
flag.Usage()
fmt.Println("TASKTYPE",TaskType)
for {
fmt.Println("time", TaskType, " ---", time.Now())
time.Sleep(16 * time.Second)

}
}
go build -o TFlag.exe

package main

import (
"fmt"
"os"
)

func main() {
p, e := os.StartProcess("TFlag.exe", []string{"CommandLine.Parse(os.Args[1:])","--TASKTYPE=TRAPCAP"}, &os.ProcAttr{Files: []*os.File{os.Stdin, os.Stdout, os.Stderr}})
fmt.Println(p, e)
}








原文地址:https://www.cnblogs.com/rsapaper/p/15513423.html