goctl quickstart

Overview

goctl quickstart is used to quickly generate api, rpc services and start up and can help first go-zero developers quickly get to get started. Once demo starts and accesses, developers can read a simple source directory of services, go-zero engineering structure to get an overview of go-zero.

goctl quickstart directive

  1. $ goctl quickstart --help
  2. quickly start a project
  3. Usage:
  4. goctl quickstart [flags]
  5. Flags:
  6. -h, --help help for quickstart
  7. -t, --service-type string specify the service type, supported values: [mono, micro] (default "mono")
goctl quickstart - 图1 Parameter fieldgoctl quickstart - 图2 Parameter Typegoctl quickstart - 图3 Required?goctl quickstart - 图4 Default valuegoctl quickstart - 图5 Parameter Description
service-typestringNOmonoGenerate demo service type, mono:mono service, micro:microservice

Examples

  • 生成单体服务
  • 生成微服务

If the mono type is selected, goctl generates a minimized HTTP service and starts the HTTP service.

  1. Generate code
  1. # enter user home
  2. $ cd ~
  3. # mkdir
  4. $ mkdir quickstart && cd quickstart
  5. # generate mono service
  6. $ goctl quickstart --service-type mono
  7. go: creating new go.mod: module greet
  8. >> Generating quickstart api project...
  9. Done.
  10. >> go mod tidy
  11. go: finding module for package github.com/zeromicro/go-zero/core/logx
  12. go: finding module for package github.com/zeromicro/go-zero/rest
  13. go: finding module for package github.com/zeromicro/go-zero/rest/httpx
  14. go: finding module for package github.com/zeromicro/go-zero/core/conf
  15. go: found github.com/zeromicro/go-zero/core/conf in github.com/zeromicro/go-zero v1.4.3
  16. go: found github.com/zeromicro/go-zero/rest in github.com/zeromicro/go-zero v1.4.3
  17. go: found github.com/zeromicro/go-zero/rest/httpx in github.com/zeromicro/go-zero v1.4.3
  18. go: found github.com/zeromicro/go-zero/core/logx in github.com/zeromicro/go-zero v1.4.3
  19. >> Ready to start an API server...
  20. >> Run 'curl http://127.0.0.1:8888/ping' after service startup...
  21. Starting server at 127.0.0.1:8888...
  1. curl

open a new terminal, execute curl test

  1. $ curl -i http://127.0.0.1:8888/ping
  2. HTTP/1.1 200 OK
  3. Content-Type: application/json; charset=utf-8
  4. Traceparent: 00-2102678b8c7c5906b792c618b054c9a1-60194e95cceff37f-00
  5. Date: Fri, 06 Jan 2023 08:52:30 GMT
  6. Content-Length: 14
  7. {"msg":"pong"}%

If the micro type is selected, goctl generates a gRPC service and a HTTP service, then starts gRPC service to communicate with the HTTP service.

  1. Generate service
  1. # enter user home
  2. $ cd ~
  3. # mkdir
  4. $ mkdir quickstart && cd quickstart
  5. # generate micro service
  6. $ goctl quickstart --service-type micro
  7. Detected that the "/Users/keson/quickstart/greet" already exists, do you clean up? [y: YES, n: NO]: y
  8. Clean workspace...
  9. go: creating new go.mod: module greet
  10. >> Generating quickstart zRPC project...
  11. [goctl-env]: preparing to check env
  12. [goctl-env]: looking up "protoc"
  13. [goctl-env]: "protoc" is installed
  14. [goctl-env]: looking up "protoc-gen-go"
  15. [goctl-env]: "protoc-gen-go" is installed
  16. [goctl-env]: looking up "protoc-gen-go-grpc"
  17. [goctl-env]: "protoc-gen-go-grpc" is installed
  18. [goctl-env]: congratulations! your goctl environment is ready!
  19. [command]: protoc greet.proto --go_out . --go-grpc_out .
  20. Done.
  21. >> Generating quickstart api project...
  22. Done.
  23. >> go mod tidy
  24. go: finding module for package github.com/zeromicro/go-zero/core/conf
  25. go: finding module for package github.com/zeromicro/go-zero/core/logx
  26. go: finding module for package github.com/zeromicro/go-zero/core/service
  27. go: finding module for package google.golang.org/grpc/reflection
  28. go: finding module for package github.com/zeromicro/go-zero/zrpc
  29. go: finding module for package github.com/zeromicro/go-zero/rest
  30. go: finding module for package github.com/zeromicro/go-zero/rest/httpx
  31. go: finding module for package google.golang.org/grpc
  32. go: finding module for package google.golang.org/grpc/codes
  33. go: finding module for package google.golang.org/grpc/status
  34. go: finding module for package google.golang.org/protobuf/reflect/protoreflect
  35. go: finding module for package google.golang.org/protobuf/runtime/protoimpl
  36. go: found github.com/zeromicro/go-zero/core/conf in github.com/zeromicro/go-zero v1.4.3
  37. go: found github.com/zeromicro/go-zero/rest in github.com/zeromicro/go-zero v1.4.3
  38. go: found github.com/zeromicro/go-zero/rest/httpx in github.com/zeromicro/go-zero v1.4.3
  39. go: found github.com/zeromicro/go-zero/core/logx in github.com/zeromicro/go-zero v1.4.3
  40. go: found github.com/zeromicro/go-zero/zrpc in github.com/zeromicro/go-zero v1.4.3
  41. go: found github.com/zeromicro/go-zero/core/service in github.com/zeromicro/go-zero v1.4.3
  42. go: found google.golang.org/grpc in google.golang.org/grpc v1.51.0
  43. go: found google.golang.org/grpc/reflection in google.golang.org/grpc v1.51.0
  44. go: found google.golang.org/grpc/codes in google.golang.org/grpc v1.51.0
  45. go: found google.golang.org/grpc/status in google.golang.org/grpc v1.51.0
  46. go: found google.golang.org/protobuf/reflect/protoreflect in google.golang.org/protobuf v1.28.1
  47. go: found google.golang.org/protobuf/runtime/protoimpl in google.golang.org/protobuf v1.28.1
  48. >> Ready to start a zRPC server...
  49. >> Ready to start an API server...
  50. >> Run 'curl http://127.0.0.1:8888/ping' after service startup...
  51. Starting rpc server at 127.0.0.1:8080...
  52. Starting server at 127.0.0.1:8888...
  1. curl

Open a new terminal and execute curl test

  1. $ curl -i http://127.0.0.1:8888/ping
  2. HTTP/1.1 200 OK
  3. Content-Type: application/json; charset=utf-8
  4. Traceparent: 00-b7c7bd5b28a49ab97960ed83d2888f1a-4da22e03825041e7-00
  5. Date: Fri, 06 Jan 2023 08:55:33 GMT
  6. Content-Length: 14
  7. {"msg":"pong"}