暂时忽略rpc的安装问题
构建rpc-server
package main import ( "fmt" "net" "net/http" "net/rpc" ) type User struct { } func (u *User) GetUser(name string, out_data *string) error { *out_data = "hello "+name return nil } func main() { user := new(User) rpc.Register(user) rpc.HandleHTTP() listen, err := net.Listen("tcp","127.0.0.1:8080") fmt.Println("监听...") if err != nil{ fmt.Println(err) } http.Serve(listen,nil) }
构建rpc-client
package main import ( "fmt" "net/rpc" ) func main() { client,err:=rpc.DialHTTP("tcp","127.0.0.1:8080") if err !=nil{ fmt.Println(err) } var data string err1 :=client.Call("User.GetUser","taipi",&data) if err !=nil{ fmt.Println(err1) } fmt.Println("res->",data) }
标签:err,nil,fmt,rpc,Println,net From: https://www.cnblogs.com/topass123/p/16991854.html