Last active
August 29, 2015 13:57
-
-
Save golangfan/9598805 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"net/rpc" | |
) | |
func main() { | |
client, err := rpc.DialHTTP("tcp", "127.0.0.1:1234") | |
if err != nil { | |
log.Fatal("connect rpc server error:", err) | |
} | |
var reply string | |
err = client.Call("Echo.Hi", "Hello World", &reply) | |
if err != nil { | |
log.Fatal("RPC error:", err) | |
} | |
fmt.Println(reply) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net" | |
"net/http" | |
"net/rpc" | |
) | |
type Echo int | |
func (t *Echo) Hi(args string, reply *string) error { | |
*reply = "RPC Server:" + args | |
return nil | |
} | |
func main() { | |
// 注册RPC服务 | |
rpc.Register(new(Echo)) | |
rpc.HandleHTTP() | |
l, e := net.Listen("tcp", ":1234") | |
if e != nil { | |
log.Fatal("listen error:", e) | |
} | |
http.Serve(l, nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
被注册对象的类型所有满足以下规则的方法会被导出到 RPC 服务接口:
func (t *T) MethodName(argType T1, replyType *T2) error