当前位置: 首页 > news >正文

微信公众号的微网站怎么做网站页面设计与制作实践

微信公众号的微网站怎么做,网站页面设计与制作实践,wordpress文章到qq群,wordpress自动增加阅读数代码第 3 章#xff1a;GO 的接口和抽象 拓展篇 - CRUD 接口实现示例 在前面的第3章中#xff0c;我们用简单的代码展示了GO的接口和抽象方法#xff0c;但是代码的示例较少#xff0c;部分同学可能会觉得理解起来比较抽象。因此在本章中#xff0c;我们将通过一个具体的例子…第 3 章GO 的接口和抽象 拓展篇 - CRUD 接口实现示例 在前面的第3章中我们用简单的代码展示了GO的接口和抽象方法但是代码的示例较少部分同学可能会觉得理解起来比较抽象。因此在本章中我们将通过一个具体的例子来演示如何使用 GO 语言的接口来实现抽象化的设计。我们将定义一个 CrudInterface 接口该接口将提供 CRUD创建、读取、更新、删除操作的通用方法。然后我们将为两种不同的存储系统MySQL 和 Redis提供该接口的具体实现。此外我们还将展示如何根据传入参数中的 URI 协议来动态选择使用哪种存储实现。 #mermaid-svg-2purIEjrJkiZ8C6N {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-2purIEjrJkiZ8C6N .error-icon{fill:#552222;}#mermaid-svg-2purIEjrJkiZ8C6N .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-2purIEjrJkiZ8C6N .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-2purIEjrJkiZ8C6N .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-2purIEjrJkiZ8C6N .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-2purIEjrJkiZ8C6N .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-2purIEjrJkiZ8C6N .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-2purIEjrJkiZ8C6N .marker{fill:#333333;stroke:#333333;}#mermaid-svg-2purIEjrJkiZ8C6N .marker.cross{stroke:#333333;}#mermaid-svg-2purIEjrJkiZ8C6N svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-2purIEjrJkiZ8C6N g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-2purIEjrJkiZ8C6N g.classGroup text .title{font-weight:bolder;}#mermaid-svg-2purIEjrJkiZ8C6N .nodeLabel,#mermaid-svg-2purIEjrJkiZ8C6N .edgeLabel{color:#131300;}#mermaid-svg-2purIEjrJkiZ8C6N .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-2purIEjrJkiZ8C6N .label text{fill:#131300;}#mermaid-svg-2purIEjrJkiZ8C6N .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-2purIEjrJkiZ8C6N .classTitle{font-weight:bolder;}#mermaid-svg-2purIEjrJkiZ8C6N .node rect,#mermaid-svg-2purIEjrJkiZ8C6N .node circle,#mermaid-svg-2purIEjrJkiZ8C6N .node ellipse,#mermaid-svg-2purIEjrJkiZ8C6N .node polygon,#mermaid-svg-2purIEjrJkiZ8C6N .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-2purIEjrJkiZ8C6N .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-2purIEjrJkiZ8C6N g.clickable{cursor:pointer;}#mermaid-svg-2purIEjrJkiZ8C6N g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-2purIEjrJkiZ8C6N g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-2purIEjrJkiZ8C6N .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-2purIEjrJkiZ8C6N .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-2purIEjrJkiZ8C6N .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-2purIEjrJkiZ8C6N .dashed-line{stroke-dasharray:3;}#mermaid-svg-2purIEjrJkiZ8C6N #compositionStart,#mermaid-svg-2purIEjrJkiZ8C6N .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-2purIEjrJkiZ8C6N #compositionEnd,#mermaid-svg-2purIEjrJkiZ8C6N .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-2purIEjrJkiZ8C6N #dependencyStart,#mermaid-svg-2purIEjrJkiZ8C6N .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-2purIEjrJkiZ8C6N #dependencyStart,#mermaid-svg-2purIEjrJkiZ8C6N .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-2purIEjrJkiZ8C6N #extensionStart,#mermaid-svg-2purIEjrJkiZ8C6N .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-2purIEjrJkiZ8C6N #extensionEnd,#mermaid-svg-2purIEjrJkiZ8C6N .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-2purIEjrJkiZ8C6N #aggregationStart,#mermaid-svg-2purIEjrJkiZ8C6N .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-2purIEjrJkiZ8C6N #aggregationEnd,#mermaid-svg-2purIEjrJkiZ8C6N .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-2purIEjrJkiZ8C6N .edgeTerminals{font-size:11px;}#mermaid-svg-2purIEjrJkiZ8C6N :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Implements Implements CrudInterface Create(ctx context.Context, key string, value interface) : error Read(ctx context.Context, key string) (interface, error) Update(ctx context.Context, key string, value interface) : error Delete(ctx context.Context, key string) : error MySQLStore Create(ctx context.Context, key string, value interface) : error Read(ctx context.Context, key string) (interface, error) Update(ctx context.Context, key string, value interface) : error Delete(ctx context.Context, key string) : error RedisStore Create(ctx context.Context, key string, value interface) : error Read(ctx context.Context, key string) (interface, error) Update(ctx context.Context, key string, value interface) : error Delete(ctx context.Context, key string) : error 3.1 定义 CrudInterface 接口 首先我们定义 CrudInterface 接口它包含 CRUD 操作的方法 package mainimport (contexterrors )// CrudInterface 定义了CRUD操作的方法 type CrudInterface interface {Create(ctx context.Context, key string, value interface{}) errorRead(ctx context.Context, key string) (interface{}, error)Update(ctx context.Context, key string, value interface{}) errorDelete(ctx context.Context, key string) error }3.2 实现 MySQL 存储 我们将创建一个 MySQLStore 结构体它实现了 CrudInterface 接口 type MySQLStore struct {// 此处包含连接数据库所需的字段 }func (m *MySQLStore) Create(ctx context.Context, key string, value interface{}) error {// 实现创建逻辑return nil }func (m *MySQLStore) Read(ctx context.Context, key string) (interface{}, error) {// 实现读取逻辑return nil, nil }func (m *MySQLStore) Update(ctx context.Context, key string, value interface{}) error {// 实现更新逻辑return nil }func (m *MySQLStore) Delete(ctx context.Context, key string) error {// 实现删除逻辑return nil }3.3 实现 Redis 存储 同样地我们将创建一个 RedisStore 结构体它也实现了 CrudInterface 接口 type RedisStore struct {// 此处包含连接Redis所需的字段 }func (r *RedisStore) Create(ctx context.Context, key string, value interface{}) error {// 实现创建逻辑return nil }func (r *RedisStore) Read(ctx context.Context, key string) (interface{}, error) {// 实现读取逻辑return nil, nil }func (r *RedisStore) Update(ctx context.Context, key string, value interface{}) error {// 实现更新逻辑return nil }func (r *RedisStore) Delete(ctx context.Context, key string) error {// 实现删除逻辑return nil }3.4 创建存储的工厂函数 我们将编写一个工厂函数 NewCrudStore它根据 URI 协议来创建和返回相应的 CrudInterface 实现 func NewCrudStore(uri string) (CrudInterface, error) {switch uri {case mysql://default:return MySQLStore{}, nilcase redis://default:return RedisStore{}, nildefault:return nil, errors.New(unsupported URI scheme)} }3.5 主程序中的动态选择 在主程序中我们将使用传入的 URI 参数来动态选择和创建存储实现 func main() {uri : mysql://default // 这可以是命令行参数或其他配置来源store, err : NewCrudStore(uri)if err ! nil {log.Fatalf(Failed to create CRUD store: %v, err)}// 现在可以使用store进行CRUD操作// 例如: store.Create(ctx, key, value) }3.6 用时序图来描述一种可能的行为 基于上面的代码我们可以得出一种可能的行为当客户端要创建一条记录时NewCrudStore能过传入的URL参数得知这次的创建是要写入redis于是最终接口调用了RedisStore保存了数据。 #mermaid-svg-vAb3HoVJhbvUTbTx {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-vAb3HoVJhbvUTbTx .error-icon{fill:#552222;}#mermaid-svg-vAb3HoVJhbvUTbTx .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-vAb3HoVJhbvUTbTx .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-vAb3HoVJhbvUTbTx .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-vAb3HoVJhbvUTbTx .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-vAb3HoVJhbvUTbTx .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-vAb3HoVJhbvUTbTx .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-vAb3HoVJhbvUTbTx .marker{fill:#333333;stroke:#333333;}#mermaid-svg-vAb3HoVJhbvUTbTx .marker.cross{stroke:#333333;}#mermaid-svg-vAb3HoVJhbvUTbTx svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-vAb3HoVJhbvUTbTx .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-vAb3HoVJhbvUTbTx text.actortspan{fill:black;stroke:none;}#mermaid-svg-vAb3HoVJhbvUTbTx .actor-line{stroke:grey;}#mermaid-svg-vAb3HoVJhbvUTbTx .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-vAb3HoVJhbvUTbTx .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-vAb3HoVJhbvUTbTx #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-vAb3HoVJhbvUTbTx .sequenceNumber{fill:white;}#mermaid-svg-vAb3HoVJhbvUTbTx #sequencenumber{fill:#333;}#mermaid-svg-vAb3HoVJhbvUTbTx #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-vAb3HoVJhbvUTbTx .messageText{fill:#333;stroke:#333;}#mermaid-svg-vAb3HoVJhbvUTbTx .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-vAb3HoVJhbvUTbTx .labelText,#mermaid-svg-vAb3HoVJhbvUTbTx .labelTexttspan{fill:black;stroke:none;}#mermaid-svg-vAb3HoVJhbvUTbTx .loopText,#mermaid-svg-vAb3HoVJhbvUTbTx .loopTexttspan{fill:black;stroke:none;}#mermaid-svg-vAb3HoVJhbvUTbTx .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-vAb3HoVJhbvUTbTx .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-vAb3HoVJhbvUTbTx .noteText,#mermaid-svg-vAb3HoVJhbvUTbTx .noteTexttspan{fill:black;stroke:none;}#mermaid-svg-vAb3HoVJhbvUTbTx .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-vAb3HoVJhbvUTbTx .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-vAb3HoVJhbvUTbTx .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-vAb3HoVJhbvUTbTx .actorPopupMenu{position:absolute;}#mermaid-svg-vAb3HoVJhbvUTbTx .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-vAb3HoVJhbvUTbTx .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-vAb3HoVJhbvUTbTx .actor-man circle,#mermaid-svg-vAb3HoVJhbvUTbTx line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-vAb3HoVJhbvUTbTx :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 客户端 main函数 NewCrudStore工厂函数 RedisStore Redis数据库 调用创建记录 创建CrudInterface实例 传入URI参数 redis://default 实例化RedisStore 连接到Redis 连接成功 返回RedisStore实例 调用Create方法 执行SET命令 返回执行结果 返回结果给main函数 返回结果给客户端 客户端 main函数 NewCrudStore工厂函数 RedisStore Redis数据库 在这个时序图中 客户端调用main函数来创建一条新记录。main函数请求NewCrudStore工厂函数创建一个CrudInterface类型的实例。NewCrudStore工厂函数根据传入的URI参数在这个场景中是redis://default实例化一个RedisStore对象。RedisStore对象尝试连接到Redis数据库。连接成功后RedisStore的Create方法被main函数调用以创建新记录。RedisStore在Redis数据库中执行SET命令来保存新记录。Redis数据库返回执行结果给RedisStore。RedisStore将结果返回给main函数。最终main函数将结果返回给客户端。 通过这个例子我们展示了如何利用 GO 语言的接口特性来实现一个灵活的、可插拔的 CRUD 服务。程序可以根据运行时的配置动态选择使用 MySQL 或 Redis 作为后端存储而不需要修改业务逻辑代码。这种设计提高了系统的可扩展性和可维护性。
http://www.zqtcl.cn/news/983099/

相关文章:

  • 昆明网站建设猫咪科技公司资料模板
  • 网站系统开发做网站需要填什么
  • 网站的数据库丢失建筑素材网
  • 个人网站做短视频pathon能做网站开发吗
  • 客户网站制作管理系统网站程序 wap pc 同步
  • 天津手动网站建设调试百度医院网站建设
  • ppt网站源码今天哈尔滨最新通告
  • asp网站乱码广州制作网页设计
  • 调用别人网站的数据库如何开网店卖自己的东西
  • 个人网站做影视网站开发学什么专业
  • 企业名称注册查询官网入口免费seo网站推广
  • 浙江门户网站建设公司个体工商户查询
  • 做网站的注意点赛事竞猜网站开发
  • 现在流行用什么语言做网站ppt设计教程网
  • 高端网站哪种好培训机构不退钱最怕什么举报
  • 青岛个人建站模板wordpress没有链接
  • 网上学习网站有哪些厦门城乡建设局网站
  • 怎样创建网站快捷方式个人制作一个网站的费用
  • 恒信在线做彩票的是什么样的网站软件开发流程管理
  • 网站服务器地址在哪里看艺术学校网站模板
  • 郑州中心站网站建设价格标准新闻
  • 电子商务网站管理互联网营销师主要做什么
  • 门户网站指的是什么凯里网络公司建设网站
  • 网站接入服务商查询0建设营销型网站步骤
  • 长沙如何做百度的网站小型网站建设实训教程
  • 昆明网络公司网站网站建设经费请示
  • 手机端网站欣赏wordpress 文章rss
  • 做网站一定要实名认证吗国外免费空间网站申请
  • 阿里云网站空间主机长春网站建设设计
  • 龙华网站建设yihekj长沙招聘网站制作