安装与使用

优质
小牛编辑
130浏览
2023-12-01
  1. go get github.com/gorilla/websocket
  2. go get github.com/valyala/fasthttp
  3. go get github.com/hprose/hprose-golang

使用

Hello 服务端

  1. package main
  2.  
  3. import (
  4. "net/http"
  5.  
  6. "github.com/hprose/hprose-golang/rpc"
  7. )
  8.  
  9. func hello(name string) string {
  10. return "Hello " + name + "!"
  11. }
  12.  
  13. func main() {
  14. service := rpc.NewHTTPService()
  15. service.AddFunction("hello", hello, rpc.Options{})
  16. http.ListenAndServe(":8080", service)
  17. }

Hello 客户端

  1. package main
  2.  
  3. import (
  4. "fmt"
  5.  
  6. "github.com/hprose/hprose-golang/rpc"
  7. )
  8.  
  9. type Stub struct {
  10. Hello func(string) (string, error)
  11. AsyncHello func(func(string, error), string) `name:"hello"`
  12. }
  13.  
  14. func main() {
  15. client := rpc.NewClient("http://127.0.0.1:8080/")
  16. var stub *Stub
  17. client.UseService(&stub)
  18. stub.AsyncHello(func(result string, err error) {
  19. fmt.Println(result, err)
  20. }, "async world")
  21. fmt.Println(stub.Hello("world"))
  22. }