-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemp.go
77 lines (77 loc) · 2 KB
/
temp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"bufio"
"fmt"
"os/exec"
)
func main() {
// What we want to calculate
calcs := make([]string, 1)
calcs[0] = "whoami"//"3*3"//
// To store the results
results := make([]string, 1)
//---------------------------------------------
//cmd := exec.Command("/usr/bin/bc")
cmd := exec.Command("./mainRef","localhost:1234","localhost:1234")
in, err := cmd.StdinPipe()
if err != nil {
panic(err)
}
defer in.Close()
out, err := cmd.StdoutPipe()
if err != nil {
panic(err)
}
defer out.Close()
// We want to read line by line
bufOut := bufio.NewReader(out)
//bufin := bufio.NewWriter(in)
//----------------------------------------------
// Start the process
if err = cmd.Start(); err != nil{
panic(err)
}
// Write the operations to the process
// for _, calc := range calcs {
_, err1 := in.Write([]byte(calcs[0] + "\n"))
if err != nil {
panic(err1)
}
// }
defer in.Close()
var id string
// Read the results from the process
for i := 0; i < 2; i++ {
result,_,err := bufOut.ReadLine()
if err != nil {
panic(err)
}
results[0] = string(result)
id = results[0]
fmt.Println(results[0])
}
// See what was calculated
// for _, result := range results {
// fmt.Println(result)
// }
// We want to read line by line
// bufOut = bufio.NewReader(out)
//--------------------------------------------
// Write the operations to the process
_, err = in.Write([]byte("get_contact "+id+ "\n"))
if err != nil {
panic(err)
}
// Read the results from the process
// for i := 0; i < len(results); i++ {
result, _, err := bufOut.ReadLine()
if err != nil {
panic(err)
}
results[0] = string(result)
// }
// See what was calculated
for _, result := range results {
fmt.Println(result)
}
}