July 11, 2015

Simple Golang Example of os.exec on Windows

package main

import (
"fmt"
"log"
"os/exec"
"strings"
)

func doCmd(cmd string) string {
parts := strings.Fields(cmd)
head := parts[0]
parts = parts[1:len(parts)]
out, err := exec.Command(head, parts...).Output()
if err != nil {
log.Fatal(err)
}
return string(out)
}

func main() {
cmd := "netsh wlan show networks mode=bssid"
out := doCmd(cmd)
fmt.Printf("%s\n", out)
}

No comments:

Post a Comment