Skip to main content

goctl quickstart

概述

goctl quickstart 用于快速生成 api、rpc 服务并启动,可以帮助初入 go-zero 的开发者快速上手,开发者在 demo 启动并访问成功后,可以阅读源码简单的了解服务目录结构,go-zero 工程结构,以此来对 go-zero 有个大致的了解。

goctl quickstart 指令

$ goctl quickstart --help
quickly start a project

Usage:
goctl quickstart [flags]

Flags:
-h, --help help for quickstart
-t, --service-type string specify the service type, supported values: [mono, micro] (default "mono")
参数字段 参数类型 是否必填 默认值 参数说明
service-typestringNOmono生成 demo 服务类型,mono:单体服务,micro:微服务

使用示例

如果选择 mono 类型,goctl 会生成一个最小化的 HTTP 服务,然后启动 HTTP 服务。

  1. 快速生成服务

    # 进入用户 Home 目录
    $ cd ~

    # 创建目录
    $ mkdir quickstart && cd quickstart

    # 生成单体服务
    $ goctl quickstart --service-type mono
    go: creating new go.mod: module greet
    >> Generating quickstart api project...
    Done.
    >> go mod tidy
    go: finding module for package github.com/zeromicro/go-zero/core/logx
    go: finding module for package github.com/zeromicro/go-zero/rest
    go: finding module for package github.com/zeromicro/go-zero/rest/httpx
    go: finding module for package github.com/zeromicro/go-zero/core/conf
    go: found github.com/zeromicro/go-zero/core/conf in github.com/zeromicro/go-zero v1.4.3
    go: found github.com/zeromicro/go-zero/rest in github.com/zeromicro/go-zero v1.4.3
    go: found github.com/zeromicro/go-zero/rest/httpx in github.com/zeromicro/go-zero v1.4.3
    go: found github.com/zeromicro/go-zero/core/logx in github.com/zeromicro/go-zero v1.4.3
    >> Ready to start an API server...
    >> Run 'curl http://127.0.0.1:8888/ping' after service startup...
    Starting server at 127.0.0.1:8888...
  2. curl 测试

新打开一个终端,执行 curl 测试

$ curl -i http://127.0.0.1:8888/ping
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Traceparent: 00-2102678b8c7c5906b792c618b054c9a1-60194e95cceff37f-00
Date: Fri, 06 Jan 2023 08:52:30 GMT
Content-Length: 14

{"msg":"pong"}%