安装与使用
优质
小牛编辑
130浏览
2023-12-01
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"))
- }