There are many ways to do this Interoperability :
1- I recommend to use standard golang package (Lib) calling, instead of Interoperability, if you have source files of both sides.
2- using "os/exec": if you don't have source, and you have only binary,
or you may pass args through files or text args:
you may pass args like this:
package main
import (
"fmt"
"os"
)
func main() {
fmt.Println(os.Args[0]) // fileNameAndPath
}
or using "flag" std lib:
// flags.exe -h
package main
import (
"flag"
"fmt"
)
func main() {
namePtr := flag.String("name", "AR", "name")
agePtr := flag.Int("age", 3700, "age")
flag.Parse()
fmt.Println(*namePtr, *agePtr) //AR 3700
}
/*
Usage of flags.exe:
-age int
age (default 3700)
-name string
name (default "AR")
*/
which will provides -h for help.
and you can call another binary program or golang compiler itself like this:
package main
import (
"log"
"os/exec"
)
func main() {
cmnd := exec.Command("main.exe", "arg")
//cmnd.Run() // and wait
cmnd.Start()
log.Println("log")
}
3- another way is calling an external program by using stdin /stdout.
in this way you can send binary data over stdin/out:
Here file "a" calls binary file "b" and sends and receives through stdin/stdout:
this is my conversion from :
http://erlang.org/doc/tutorial/c_port.html
(you may use os named pipe)
file a:
// a
package main
import (
"fmt"
"log"
"os/exec"
"runtime"
"time"
)
var cout chan []byte = make(chan []byte)
var cin chan []byte = make(chan []byte)
var exit chan bool = make(chan bool)
func Foo(x byte) byte { return call_port([]byte{1, x}) }
func Bar(y byte) byte { return call_port([]byte{2, y}) }
func Exit() byte { return call_port([]byte{0, 0}) }
func call_port(s []byte) byte {
cout <- s
s = <-cin
return s[1]
}
func start() {
fmt.Println("start")
cmd := exec.Command("../b/b")
stdin, err := cmd.StdinPipe()
if err != nil {
log.Fatal(err)
}
stdout, err2 := cmd.StdoutPipe()
if err2 != nil {
log.Fatal(err2)
}
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
defer stdin.Close()
defer stdout.Close()
for {
select {
case s := <-cout:
stdin.Write(s)
buf := make([]byte, 2)
runtime.Gosched()
time.Sleep(100 * time.Millisecond)
stdout.Read(buf)
cin <- buf
case b := <-exit:
if b {
fmt.Printf("Exit")
return //os.Exit(0)
}
}
}
}
func main() {
go start()
runtime.Gosched()
fmt.Println("30+1=", Foo(30)) //30+1= 31
fmt.Println("2*40=", Bar(40)) //2*40= 80
Exit()
exit <- true
}
file b:
// b
package main
import (
"log"
"os"
)
func foo(x byte) byte { return x + 1 }
func bar(y byte) byte { return y * 2 }
func ReadByte() byte {
b1 := make([]byte, 1)
for {
n, _ := os.Stdin.Read(b1)
if n == 1 {
return b1[0]
}
}
}
func WriteByte(b byte) {
b1 := []byte{b}
for {
n, _ := os.Stdout.Write(b1)
if n == 1 {
return
}
}
}
func main() {
var res byte
for {
fn := ReadByte()
log.Println("fn=", fn)
arg := ReadByte()
log.Println("arg=", arg)
if fn == 1 {
res = foo(arg)
} else if fn == 2 {
res = bar(arg)
} else if fn == 0 {
return //exit
} else {
res = fn //echo
}
WriteByte(1)
WriteByte(res)
}
}
4 - another way is using "net/rpc", this is best way for calling another function from another program.
sample:
// rpc
package main
import (
"fmt"
"net"
"net/rpc"
"runtime"
"sync"
)
var wg sync.WaitGroup
type Server struct{}
func (this *Server) Add(u [2]int64, reply *int64) error {
*reply = u[0] + u[1]
return nil
}
func server() {
fmt.Println("server: Hi")
rpc.Register(new(Server))
ln, err := net.Listen("tcp", "127.0.0.1:12345")
if err != nil {
fmt.Println(err)
return
}
for {
c, err := ln.Accept()
if err != nil {
continue
}
go rpc.ServeConn(c)
}
}
func client() {
wg.Add(1)
c, err := rpc.Dial("tcp", "127.0.0.1:12345")
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Connected...")
var result int64
err = c.Call("Server.Add", [2]int64{10, 20}, &result)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Server.Add(10,20) =", result)
}
wg.Done()
}
func main() {
go server()
runtime.Gosched()
go client()
runtime.Gosched()
wg.Wait()
fmt.Println("Bye")
}
/*output:
server: Hi
Connected...
Server.Add(10,20) = 30
Bye
*/