Good afternoon,
I'm trying to grab processes id's with my limited knowledge on golang and the below is what I've come up with:
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
)
func main() {
cmd := exec.Command("tasklist.exe")
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
f, err := os.Create("data.txt")
if err != nil {
log.Fatal(err)
}
defer f.Close()
val := out
data := []byte(val)
_, err2 := f.Write(data)
if err2 != nil {
log.Fatal(err2)
}
val2 := " and red fox\n"
data2 := []byte(val2)
var idx int64 = int64(len(data))
_, err3 := f.WriteAt(data2, idx)
if err3 != nil {
log.Fatal(err3)
}
fmt.Println("done")
/* ioutil.ReadFile returns []byte, error */
explorer, err := ioutil.ReadFile("data.txt")
/* ... omitted error check..and please add ... */
/* find index of newline */
file := string(data)
line := 0
/* func Split(s, sep string) []string */
temp := strings.Split(file, "\n")
for _, item := range temp {
fmt.Println("[", line, "]\t", item)
line++
}
fmt.Println(explorer)
}
My Main issue is that i keep running into the same wall where ioutil won't let me assign a value before reading the file.
Is anyone able to help me out here?
I don't know what's not working in your original example, but a few minutes of experimentation and reading the docs for tasklist.exe gave me something better. The main improvement is letting
tasklist.exe
do the heavy lifting by giving it args.