前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >搞定基于go-zero微服务架构的博客系统(2)

搞定基于go-zero微服务架构的博客系统(2)

原创
作者头像
海风极客
发布2023-12-16 22:15:23
1730
发布2023-12-16 22:15:23
举报
文章被收录于专栏:扯编程的淡扯编程的淡

Go-Zero是一个基于Go语言的微服务框架,它提供了许多功能和工具,包括代码生成器。使用Go-Zero的代码生成器可以快速生成接口的定义和实现。

以下是使用Go-Zero生成接口的操作过程:

安装Go-Zero:首先,确保你的计算机上已经安装了Go语言环境。然后,通过执行以下命令安装Go-Zero:

代码语言:shell
复制
go get -u github.com/zeromicro/go-zero 

定义接口:在Go语言中,接口是一种定义对象行为的类型。你可以使用Go语言的标准库中的interface关键字来定义接口。

代码语言:go
复制
syntax = "v1"

type (
	ArticleCreateReq {
		CaregoryId    int32  `json:"caregory_id"`
		Title         string `json:"title"`
		Summary       string `json:"summary"`
		Content       string `json:"content"`
		CoverImageUrl string `json:"cover_image_url"`
	}
	ArticleCreateResp {
		ArticleId int32 `json:"article_id"`
	}

	ArticleFetchReq {
		ArticleId int32 `json:"article_id"`
	}
	ArticleFetchResp {
		UserId        int32  `json:"user_id"`
		CaregoryId    int32  `json:"caregory_id"`
		Title         string `json:"title"`
		Summary       string `json:"summary"`
		Content       string `json:"content"`
		CoverImageUrl string `json:"cover_image_url"`
	}

	ArticleFetchListReq {
	}
	ArticleFetchListResp {
		ArticleList []ArticleFetchResp `json:"article_list"`
	}

	ArticleUpdateReq {
		ArticleId     int32  `json:"article_id"`
		CaregoryId    int32  `json:"caregory_id"`
		Title         string `json:"title"`
		Summary       string `json:"summary"`
		Content       string `json:"content"`
		CoverImageUrl string `json:"cover_image_url"`
	}
	ArticleUpdateResp {
		ArticleId int32 `json:"article_id"`
	}

	ArticleDeleteReq {
		ArticleId int32 `json:"article_id"`
	}
	ArticleDeleteResp {
		ArticleId int32 `json:"article_id"`
	}
)

type (
	CategoryCreateReq {
		Name string `json:"name"`
	}
	CategoryCreateResp {
		CategoryId int32 `json:"category_id"`
	}

	CategoryFetchReq {
		CategoryId int32 `json:"category_id"`
	}
	CategoryFetchResp {
		CategoryId int32  `json:"category_id"`
		Name       string `json:"name"`
	}

	CategoryDeleteReq {
		CategoryId int32 `json:"category_id"`
	}
	CategoryDeleteResp {
		CategoryId int32 `json:"category_id"`
	}

	CategoryFetchListReq {
	}
	CategoryFetchListResp {
		CategoryList []CategoryFetchResp `json:"category_list"`
	}

	CategoryUpdateReq {
		CategoryId int32  `json:"category_id"`
		Name       string `json:"name"`
	}
	CategoryUpdateResp {
		CategoryId int32 `json:"category_id"`
	}
)

type (
	ShareCreateReq {
		ShareType    int32  `json:"share_type"`
		Title        string `json:"title"`
		FilePath     string `json:"file_path"`
		Description  string `json:"description"`
		ResourcesUrl string `json:"resources_url"`
		LogoUrl      string `json:"logo_url"`
	}
	ShareCreateResp {
		ShareId int32 `json:"share_id"`
	}

	ShareUpdateReq {
		UserId       int32  `json:"user_id"`
		ShareId      int32  `json:"share_id"`
		ShareType    int32  `json:"share_type"`
		Title        string `json:"title"`
		FilePath     string `json:"file_path"`
		Description  string `json:"description"`
		ResourcesUrl string `json:"resources_url"`
		LogoUrl      string `json:"logo_url"`
	}
	ShareUpdateResp {
		ShareId int32 `json:"share_id"`
	}

	ShareDeleteReq {
		ShareId int32 `json:"share_id"`
	}
	ShareDeleteResp {
		ShareId int32 `json:"share_id"`
	}

	ShareFetchReq {
		ShareId int32 `json:"share_id"`
	}
	ShareFetchResp {
		UserId       int32  `json:"user_id"`
		ShareId      int32  `json:"share_id"`
		ShareType    int32  `json:"share_type"`
		Title        string `json:"title"`
		FilePath     string `json:"file_path"`
		Description  string `json:"description"`
		ResourcesUrl string `json:"resources_url"`
		LogoUrl      string `json:"logo_url"`
	}

	ShareListReq {
	}
	ShareListResp {
		ShareList []ShareFetchResp `json:"share_list"`
	}
)

@server(
	prefix: /v1
	group: article
)
service geek-api {
	@handler Create
	post /article/create (ArticleCreateReq) returns (ArticleCreateResp)

	@handler Fetch
	get /article/fetch (ArticleFetchReq) returns (ArticleFetchResp)

	@handler List
	get /article/list (ArticleFetchListReq) returns (ArticleFetchListResp)

	@handler Update
	post /article/update (ArticleUpdateReq) returns (ArticleUpdateResp)

	@handler Delete
	post /article/delete (ArticleDeleteReq) returns (ArticleDeleteResp)
}

@server(
	prefix: /v1
	group: category
)
service geek-api {
	@handler List
	get /category/list (CategoryFetchListReq) returns (CategoryFetchListResp)

	@handler Update
	post /category/update (CategoryUpdateReq) returns (CategoryUpdateResp)

	@handler Info
	get /category/info (CategoryFetchReq) returns (CategoryFetchResp)

	@handler Add
	post /category/add (CategoryCreateReq) returns (CategoryCreateResp)

	@handler Delete
	post /category/delete (CategoryDeleteReq) returns (CategoryDeleteResp)
}

@server(
	prefix: /v1
	group: share
)
service geek-api {
	@handler List
	get /share/list (ShareListReq) returns (ShareListResp)

	@handler Update
	post /share/update (ShareUpdateReq) returns (ShareUpdateResp)

	@handler Info
	get /share/info (ShareFetchReq) returns (ShareFetchResp)

	@handler Add
	post /share/add (ShareCreateReq) returns (ShareCreateResp)

	@handler Delete
	post /share/delete (ShareDeleteReq) returns (ShareDeleteResp)
}

生成接口代码:使用Go-Zero的代码生成器可以自动生成接口的实现代码。在终端中执行以下命令:

代码语言:shell
复制
goctl api go --api web.api --dir .

这将自动生成与接口定义相对应的代码。Go-Zero会根据接口定义生成相应的结构体和方法,并添加必要的依赖项。

查看生成的代码:生成的代码将保存在当前目录下的generate文件夹中。进入该文件夹,可以看到生成的代码文件。你可以查看生成的代码以确认是否符合预期。

使用生成的代码:将生成的代码文件导入到你的项目中,并按照生成的代码进行使用。你可以使用生成的接口和方法来编写业务逻辑代码,而无需手动编写实现细节。

这就是使用Go-Zero生成接口的基本操作过程。通过这种方式,你可以快速地创建和实现接口,从而减少开发时间和错误。

我正在参与2023腾讯技术创作特训营第四期有奖征文,快来和我瓜分大奖!

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档