package main import ( "bytes" "gob" "rpc" "http" "io/ioutil" "net" "fmt" "os" ) type Test int func EncodeToBinary(value interface{}) []byte { buf := bytes.NewBuffer(make([]byte,0)) encoder := gob.NewEncoder(buf) encoder.Encode(value) return buf.Bytes() } func DecodeFromBinary(buffer []byte, value interface{}) os.Error { //fmt.Println("decodeFromBinary: ", buffer, value) buf := bytes.NewBuffer(buffer) decoder := gob.NewDecoder(buf) return decoder.Decode(value) } func (t *Test) RPC(args *int, reply *int) os.Error { *reply = 0 return nil } func main() { t := new(Test) b, err := ioutil.ReadFile("test.txt") if err == nil { var a map[int]int DecodeFromBinary(b, &a) fmt.Println(EncodeToBinary(a)) return } rpc.Register(t) rpc.HandleHTTP() l, _ := net.Listen("tcp", ":1234") go http.Serve(l, nil) client, _ := rpc.DialHTTP("tcp", "127.0.0.1:1234") i := 0 a := make(map[int]int) client.Call("Test.RPC", &i, &i) b = EncodeToBinary(a) fmt.Println(b) ioutil.WriteFile("test.txt", b, 0666) }