Location:Home >> News >> develop

PHP转Go,框架选什么?

不管我们是否基于框架去开发,不管使用什么框架去开发。6个字的内功心法一定要牢记:高内聚,低耦合。

这是一期会引起广泛讨论的内容,不亚于“PHP是世界上最好的语言”。

内功心法

不管我们是否基于框架去开发,不管使用什么框架去开发。

6个字的内功心法一定要牢记:高内聚,低耦合。

不管是单体项目还是微服务,不管是小组件还是大功能,不管是业务还是领域DDD,也都遵从这个原则:

  • (模块、功能、业务、领域)内部高内聚,
  • (模块、功能、业务、领域)之间低耦合。

PHP转Go,优选哪个框架?

答案:GoFrame

为什么?

和PHP的Laravel简直太像了,用起来太香了,很顺手。

为什么不火?

对新手不友好,有门槛。

GoFrame特点

优势:

  • 模块化
  • 高性能
  • 企业级

劣势:

  • 有学习门槛
  • 版本兼容性不好
  • 暂不支持微服务

框架选型

谁适合用GoFrame

  • PHP转Go(Laravel的同学会用起来很爽)
  • Java转Go (Spring Boot的同学会用起来很爽)
  • 比较复杂的企业级单体项目 (很规范,维护成本低,少踩很多坑)

谁不适合用GoFrame

  • 编程小白(容易被劝退)
  • 特别简单的项目用gin等轻量级框架(杀鸡焉用牛刀)
  • 非常复杂的分布式微服务项目用go-zero等微服务框架(没有任何一款框架是万能的)

GoFrame框架设计思想

这些不作为本期内容的重点,大家可以直接阅读官方文档。

我也有计划录制视频,结合我们的商业项目,站在使用者的角度去分享使用GoFrame的感受,如何理解框架的设计思想。

  • 模块化设计[1]

图片

  • 统一框架设计[2]
  • 工程开发设计[3]
  • 全链路跟踪设计[4]
  • 全错误堆栈设计[5]
  • 接口化与泛型设计[6]
  • 隐式与显式初始化[7]
  • Context相关设计[8]
  • 微服务相关设计[9]

开发流程

我梳理的 GoFrame V2最新版的开发流程

从0到1

  • 设计表结构
  • 使用gf gen dao 生成对应的 dao/do/model
  • 编写api层:定义业务侧数据结构,提供对外接口的输入/输出数据结构
  • 编写model层:定义数据侧数据结构,提供对内的数据处理的输入/输出数据结构
  • 编写logic层,自动生成service层代码。(通过配置goland File Watcher自动生成,也可以通过gf gen service手动执行脚本生成,建议前者)
  • 在service层代码生成RegisterXX()方法后,在对应的logic模块注册服务(每个模块只需要写一次)
  • 编写controller层,接收/解析用户输入的参数,调用service层的服务。
  • 注册路由,对外暴露接口,比如这个项目是编写cmd.go文件。
  • 在main.go中 加入一行 _ "project-name/internal/logic" (只需写一次)
  • 在main.go中加入一行 _ "github.com/gogf/gf/contrib/drivers/mysql/v2" (如果你使用的是mysql;只需写一次)

核心步骤

  • 编写api层:定义业务侧数据结构,提供对外接口的输入/输出数据结构
  • 编写model层:定义数据侧数据结构,提供对内的数据处理的输入/输出数据结构
  • 编写logic层,自动生成service层代码。(通过配置goland File Watcher自动生成,也可以通过gf gen service手动执行脚本生成,建议前者)
  • 编写controller层,接收/解析用户输入的参数,调用service层的服务。
  • 注册路由,对外暴露接口,比如这个项目是编写cmd.go文件。

总结

没有一个框架是万能的,也没有一个语言是万能的。

结合自己的需求去选择合适的框架,合适的开发语言。

不要被眼前暂时的得意或者失意“一叶障目”,结合实际情况,实事求是,拥抱变化,不断学习,才能“见泰山”。

面向“养家糊口”编程,能稳定赚钱的职业才是好职业。

参考资料

[1]模块化设计: https://goframe.org/pages/viewpage.action?pageId=3672891

[2]统一框架设计: https://goframe.org/pages/viewpage.action?pageId=7290658

[3]工程开发设计: https://goframe.org/pages/viewpage.action?pageId=30740161

[4]全链路跟踪设计: https://goframe.org/pages/viewpage.action?pageId=35356689

[5]全错误堆栈设计: https://goframe.org/pages/viewpage.action?pageId=35356691

[6]接口化与泛型设计: https://goframe.org/pages/viewpage.action?pageId=35356693

[7]隐式与显式初始化: https://goframe.org/pages/viewpage.action?pageId=6357066

[8]Context相关设计: https://goframe.org/pages/viewpage.action?pageId=7301813

[9]微服务相关设计: https://goframe.org/pages/viewpage.action?pageId=54100607

Special Reminder & Disclaimer:
The information of this website comes from the Internet. The content (pictures, videos and words) released by this website is mainly the content submitted by users and reproduced by users. The purpose is to convey more information, which does not mean that this website agrees with its views. The originality, statements, and content of this article have not been verified by this website. We do not guarantee or promise the authenticity, completeness, or timeliness of this article or all or part of its content or text. Please verify the relevant content on your own. This website does not assume direct or joint liability for infringement of such works. If any content on this website infringes on your rights, please contact us promptly (email: nssa@soufind.com )This site will complete the processing within 72 hours. The original content on this website cannot be reproduced without permission, or the source must be indicated when reprinting: nssa.vip Aerospace Alliance

Author:

Recommended for you

Post comments

admin

Contact Us

Contact Us

Online consultation: QQ交谈

Email: service#soufind.com

follow us on WeChat
Scan and follow us on WeChat

Scan and follow us on WeChat