execing process _ golang

In the previous  example we looked at spawning external processes. We do this when we need an external process accessible to running Go process. Sometimes we just want to completely replace the current Go process with another one. To do this we‘ll use Go‘s implementation of classic exec function

package main

import (
    "os"
    "os/exec"
    "syscall"
)

func main() {

    binary, lookErr := exec.LookPath("ls")
    if lookErr != nil {
        panic(lookErr)
    }

    args := []string{"ls", "-a", "-l", "-h"}

    env := os.Environ()

    execErr := syscall.Exec(binary, args, env)
    if execErr != nil {
        panic(execErr)
    }
}
total 3656
drwxr-xr-x  17 xjk  staff   578B Apr  1 13:19 .
drwxr-xr-x+ 30 xjk  staff   1.0K Mar  6 14:00 ..
[email protected]  1 xjk  staff   6.0K Mar 27 11:21 .DS_Store
-rw-r--r--   1 xjk  staff   838B Mar 30 13:38 Reading.go
-rw-r--r--   1 xjk  staff   553B Mar 27 13:12 URL.go
-rw-r--r--   1 xjk  staff   598B Mar 30 13:45 Writing.go
-rw-r--r--   1 xjk  staff   402B Mar 30 13:23 base64.go
-rw-r--r--   1 xjk  staff   447B Mar 31 13:28 commandLineFlags.go
-rwxr-xr-x   1 xjk  staff   1.7M Mar 31 13:21 commonLineArgument
-rw-r--r--   1 xjk  staff   209B Mar 31 13:20 commonLineArgument.go
-rw-r--r--   1 xjk  staff   261B Apr  1 13:02 environmentVariables.go
-rw-r--r--   1 xjk  staff   303B Apr  1 13:22 execingProcess.go
-rw-r--r--   1 xjk  staff   298B Mar 31 13:15 lineFilters.go
-rw-r--r--   1 xjk  staff   384B Mar 27 13:04 number_parsing.go
-rw-r--r--   1 xjk  staff   189B Mar 27 13:18 sha.go
-rw-r--r--   1 xjk  staff   720B Apr  1 13:15 spawningProcess.go
-rwxr-xr-x   1 xjk  staff   243B Mar 27 11:25 urllib001.py

总结  :

  1 : ....

时间: 2024-10-30 01:43:47

execing process _ golang的相关文章

Spawning process _ golang

Sometimes our Go programs need to spawn other, non-Go process. For example, the syntax highlighting on this site is implemented by spawning a pygmentize process from a Go program. Let's look at a few examples of spawning processes from Go package mai

errors _ golang

In Go it's idiomatic to communicate errors via an explicit, separate return value. this constrasts errors via an explicit, separate return value. This constrasts with the exceptions used in languages like Java and Ruby and the overloaded single resul

multiple return values _ golang

Go has built-in support for multiple return values. This feature is used often in idiomatic Go, for example to return both result and error values from a function package main import ( "fmt" ) func vals() (int, int) { return 3, 7 } func main() {

variadic function _ golang

Variadic functions can be called with any number of trailing arguments. For example, fmt.Println is a common variadic function package main import ( "fmt" ) func sum(nums ...int) { fmt.Println(nums, " ") total := 0 for _, num := range

if else _ golang

if else 在 golang package main import ( "fmt" ) func main() { if 7%2 == 0 { fmt.Println("7 is even") } else { fmt.Println("7 is odd") } if 8%4 == 0 { fmt.Println("8 is divisible by 4") } if num := 9; num < 0 { fmt

for _ golang

for 是 golang 唯一的 looping 结构, package main import ( "fmt" ) func main() { i := 1 for i <= 3 { fmt.Println(i) i += 1 } for j := 7; j <= 9; j++ { fmt.Println(j) } for { fmt.Println("loop") break } } 1 2 3 7 8 9 loop 总结 : 对于这个, 额, 我觉

constants _ golang

golang 中支持 长量 const or static package main import ( "fmt" "math" ) const s string = "constant" const a = 3000 func main() { const s = 3000 fmt.Println(s) const n = (3e8 / s) fmt.Println(n) fmt.Println(int64(n)) fmt.Println(ma

slices _ golang

Slices are a key data type in Go, giving a more powerful interface to sequences than arrays package main import ( "fmt" ) func main() { s := make([]string, 3) fmt.Println("emp:", s) s[0] = "a" s[1] = "b" s[2] = &quo

variables _ golang

在 golang 中, 变量是通过编译器声明和使用的,并且编译器会进行相应的类型检查 package main import ( "fmt" ) func main() { var a string = "initial" fmt.Println(a) var b, c int = 1, 2 fmt.Println(b, c) var d = true fmt.Println(d) var e int fmt.Println(e) f := "short&