{"id":19885228,"url":"https://github.com/dinstone/focus-go","last_synced_at":"2026-06-12T15:31:33.849Z","repository":{"id":62862762,"uuid":"563167893","full_name":"dinstone/focus-go","owner":"dinstone","description":"Focus-go is the go language implementation of the Focus.","archived":false,"fork":false,"pushed_at":"2024-12-24T01:57:07.000Z","size":1901,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-01T03:45:51.654Z","etag":null,"topics":["focus","rpc"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dinstone.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-11-08T03:26:49.000Z","updated_at":"2024-12-24T01:57:10.000Z","dependencies_parsed_at":"2024-06-21T12:55:22.702Z","dependency_job_id":"1a3a4413-c922-4918-b557-55aa5296faad","html_url":"https://github.com/dinstone/focus-go","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/dinstone/focus-go","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dinstone%2Ffocus-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dinstone%2Ffocus-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dinstone%2Ffocus-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dinstone%2Ffocus-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dinstone","download_url":"https://codeload.github.com/dinstone/focus-go/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dinstone%2Ffocus-go/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34251774,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-12T02:00:06.859Z","response_time":109,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["focus","rpc"],"created_at":"2024-11-12T17:33:40.116Z","updated_at":"2026-06-12T15:31:33.824Z","avatar_url":"https://github.com/dinstone.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Overview\n\n**Focus RPC** is the next generation cross language lightweight RPC framework. It can quickly and easily develop microservice applications, which greatly simplifies RPC programming.\n\n**Focus RPC** 是下一代跨平台、跨语言的轻量级RPC框架。旨在帮助开发者可以更加高效地构建和维护微服务应用程序。他们可以利用统一的接口和协议，在不同的平台和语言之间进行通信和协作，从而提高开发效率和系统可靠性，简化多平台下的RPC编程，可以很轻松地实现云端编程和移动端编程。\n\n- [focus-go](https://github.com/focus-rpc/focus-go) is the go language implementation of the Focus.\n- [focus-java](https://github.com/focus-rpc/focus-java) is the java language implementation of the Focus.\n\n---\n\n# Focus Go Implementation\n\n**focus-go** is the go language implementation of the Focus.\n\n## Install\n\n- install `protoc` at first : http://github.com/google/protobuf/releases\n- install `protoc-gen-go` and `protoc-gen-focus` :\n\n```shell\ngo install github.com/golang/protobuf/protoc-gen-go\ngo install github.com/dinstone/focus-go/protoc-gen-focus\n```\n\n## Quick Start\n\n1. create a demo project and import the focus package:\n\n```shell\n\u003e go mod init demo\n\u003e go get github.com/dinstone/focus-go\n\u003e go get github.com/golang/protobuf/protoc-gen-go\n\u003e go get github.com/dinstone/focus-go/protoc-gen-focus\n```\n\n2. under the path of the project, create a protobuf file `arith.proto`:\n\n```protobuf\nsyntax = \"proto3\";\n\npackage protobuf;\noption go_package=\"/protobuf\";\n\n// ArithService Defining Computational Digital Services\nservice ArithService {\n  // Add addition\n  rpc Add(ArithRequest) returns (ArithResponse);\n  // Sub subtraction\n  rpc Sub(ArithRequest) returns (ArithResponse);\n  // Mul multiplication\n  rpc Mul(ArithRequest) returns (ArithResponse);\n  // Div division\n  rpc Div(ArithRequest) returns (ArithResponse);\n}\n\nmessage ArithRequest {\n  int32 a = 1;\n  int32 b = 2;\n}\n\nmessage ArithResponse {\n  int32 c = 1;\n}\n```\n\n3. using `protoc` to generate code:\n\n```shell\n\u003e protoc --focus_out=. arith.proto --go_out=. arith.proto\n```\n\nat this time, two files will be generated in the directory `protobuf`: `arith.pb.go` and `arith.svr.go`\n\n4. implement the ArithService in the file `arith.svr.go` :\n\n```go\npackage protobuf\n\nimport \"errors\"\n\n// ArithService Defining Computational Digital Services\ntype ArithService struct{}\n\n// Add addition\nfunc (this *ArithService) Add(args *ArithRequest, reply *ArithResponse) error {\n\treply.C = args.A + args.B\n\treturn nil\n}\n\n// Sub subtraction\nfunc (this *ArithService) Sub(args *ArithRequest, reply *ArithResponse) error {\n\treply.C = args.A - args.B\n\treturn nil\n}\n\n// Mul multiplication\nfunc (this *ArithService) Mul(args *ArithRequest, reply *ArithResponse) error {\n\treply.C = args.A * args.B\n\treturn nil\n}\n\n// Div division\nfunc (this *ArithService) Div(args *ArithRequest, reply *ArithResponse) error {\n\tif args.B == 0 {\n\t\treturn errors.New(\"divided is zero\")\n\t}\n\treply.C = args.A / args.B\n\treturn nil\n}\n```\n\n## Server\n\nunder the path of the project, we create a file named `focus_server.go`, create a focus server and publish service.\n\n```go\npackage main\n\nimport (\n\t\"demo/protobuf\"\n\t\"log\"\n\t\"net\"\n\n\t\"github.com/dinstone/focus-go/focus\"\n)\n\nfunc main() {\n\t// server\n\tx := options.NewServerOptions(\":8082\")\n\tx.SetSerializer(serializer.Protobuf)\n\tserver := focus.NewServer(x)\n\t// register service\n\terr := server.RegisterName(\"ArithService\", new(protobuf.ArithService))\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n\t\n\tserver.Start()\n}\n```\n\n## Client\n\nunder the path of the project, we create a file named `focus_client.go`, create a focus client and call it synchronously with the `Add` function:\n\n```go\nimport (\n\t\"demo/protobuf\"\n\t\"github.com/dinstone/focus-go/focus\"\n...\n\t// client\n\tx := options.NewClientOptions(\":8082\")\n\tx.SetSerializer(serializer.Protobuf)\n\tclient := focus.NewClient(x)\n\tdefer client.Close()\n\n\tresq := protobuf.ArithRequest{A: 20, B: 5}\n\tresp := protobuf.ArithResponse{}\n\terr = client.Call(\"ArithService\", \"Add\", \u0026resq, \u0026resp)\n\tlog.Printf(\"Arith.Add(%v, %v): %v ,Error: %v\", resq.A, resq.B, resp.C, err)\n```\n\nyou can also call asynchronously, which will return a channel of type *focus.Call:\n\n```go\n\tresult := client.AsyncCall(\"ArithService\", \"Add\", \u0026resq, \u0026resp)\n\tselect {\n\tcase call := \u003c-result:\n\t\tlog.Printf(\"Arith.Add(%v, %v): %v ,Error: %v\", resq.A, resq.B, resp.C, call.Error)\n\tcase \u003c-time.After(100 * time.Microsecond):\n\t\tlog.Fatal(\"time out\")\n\t}\n```\n\nof course, you can also compress with three supported formats `gzip`, `snappy`, `zlib`:\n\n```go\nimport (\n    \"github.com/dinstone/focus-go/focus\"\n    \"github.com/dinstone/focus-go/focus/options\"\n    \"github.com/dinstone/focus-go/focus/serializer\"\n\t\"github.com/dinstone/focus-go/focus/compressor\"\n)\n\n...\n\nx := options.NewServerOptions(\":8008\")\nx.SetCompressor(compressor.Gzip)\nx.SetSerializer(serializer.Protobuf)\nclient := focus.NewClient(x)\n```\n\n## Custom Serializer\n\nIf you want to customize the serializer, you must implement the `Serializer` interface:\n\n```go\ntype Serializer interface {\n    Marshal(message interface{}) ([]byte, error)\n    Unmarshal(data []byte, message interface{}) error\n    Type() string\n}\n```\n\n`JsonSerializer` is a serializer based Json:\n\n```go\ntype JsonSerializer struct{}\n\nfunc (_ JsonSerializer) Marshal(message interface{}) ([]byte, error) {\n\treturn json.Marshal(message)\n}\n\nfunc (_ JsonSerializer) Unmarshal(data []byte, message interface{}) error {\n\treturn json.Unmarshal(data, message)\n}\n```\nnow, we can create a HelloService with the following code:\n```go\ntype HelloRequest struct {\n\tReq string `json:\"req\"`\n}\n\ntype HelloResponce struct {\n\tResp string `json:\"resp\"`\n}\n\ntype HelloService struct{}\n\nfunc (_ *HelloService) SayHello(args *HelloRequest, reply *HelloResponce) error {\n\treply.Resp = args.Req\n\treturn nil\n}\n\n```\n\nfinally, we need to set the serializer on the focus server:\n\n```go\nfunc main() {\n\n\tx := options.NewServerOptions(\":8008\")\n\tx.SetSerializer(serializer.Json)\n\tserver := focus.NewServer(x)\n\n\tserver.Register(new(HelloService))\n\n\tserver.Start()\n}\n```\n\nRemember that when the focus client calls the service, it also needs to set the serializer.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdinstone%2Ffocus-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdinstone%2Ffocus-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdinstone%2Ffocus-go/lists"}