- 安装
- 使用
- Hello 服务端
- Hello 客户端
安装
go get github.com/gorilla/websocket
go get github.com/valyala/fasthttp
go get github.com/hprose/hprose-golang
使用
Hello 服务端
- package main
- import (
- "net/http"
- "github.com/hprose/hprose-golang/rpc"
- )
- func hello(name string) string {
- return "Hello " + name + "!"
- }
- func main() {
- service := rpc.NewHTTPService()
- service.AddFunction("hello", hello, rpc.Options{})
- http.ListenAndServe(":8080", service)
- }
Hello 客户端
- package main
- import (
- "fmt"
- "github.com/hprose/hprose-golang/rpc"
- )
- type Stub struct {
- Hello func(string) (string, error)
- AsyncHello func(func(string, error), string) `name:"hello"`
- }
- func main() {
- client := rpc.NewClient("http://127.0.0.1:8080/")
- var stub *Stub
- client.UseService(&stub)
- stub.AsyncHello(func(result string, err error) {
- fmt.Println(result, err)
- }, "async world")
- fmt.Println(stub.Hello("world"))
- }
原文:
https://github.com/hprose/hprose-golang/wiki/%E5%AE%89%E8%A3%85%E4%B8%8E%E4%BD%BF%E7%94%A8