GoMall电商项目开发记录day1


前言

本人准备25届春招go后端,尝试使用GoFrame开发一款电商项目gomall。第一篇文章,记录GoFrame脚手架生成项目目录且成功运行的步骤。


一、GoFrame下载安装

限制

golang version >= 1.20

安装

本人环境:Ubuntu 22.04
源码编译安装
go install github.com/gogf/gf/cmd/gf/v2@latest

验证

在终端执行 gf -v 指令如果能打印出例如以下信息,表示已成功安装好了框架脚手架工具 👍

root@ecs-1b41 ~/p/gomall# gf -v
v2.8.3
Welcome to GoFrame!
Env Detail:
  Go Version: go1.22.11 linux/amd64
  GF Version(go.mod): cannot find go.mod
CLI Detail:
  Installed At: /root/go/bin/gf
  Built Go Version: go1.22.11
  Built GF Version: v2.8.3
Others Detail:
  Docs: https://goframe.org
  Now : 2025-02-10T18:49:20+08:00

其中的 Go/GF Version 是当前编译的二进制文件使用的 Golang 及 GoFrame 框架版本,其中的 GoFrame Version 是当前项目使用的 GoFrame 框架版本(自动检测当前目录下的 go.mod)。

二、创建项目模板

终端执行:

gf init gomall -u

生成目录结构

.
├── api
│   └── hello
│       ├── hello.go
│       └── v1
│           └── hello.go
├── go.mod
├── go.sum
├── hack
│   ├── config.yaml
│   ├── hack-cli.mk
│   └── hack.mk
├── internal
│   ├── cmd
│   │   └── cmd.go
│   ├── consts
│   │   └── consts.go
│   ├── controller
│   │   └── hello
│   │       ├── hello.go
│   │       ├── hello_new.go
│   │       └── hello_v1_hello.go
│   ├── dao
│   ├── logic
│   ├── model
│   │   ├── do
│   │   └── entity
│   ├── packed
│   │   └── packed.go
│   └── service
├── main.go
├── Makefile
├── manifest
│   ├── config
│   │   └── config.yaml
│   ├── deploy
│   │   └── kustomize
│   │       ├── base
│   │       │   ├── deployment.yaml
│   │       │   ├── kustomization.yaml
│   │       │   └── service.yaml
│   │       └── overlays
│   │           └── develop
│   │               ├── configmap.yaml
│   │               ├── deployment.yaml
│   │               └── kustomization.yaml
│   ├── docker
│   │   ├── Dockerfile
│   │   └── docker.sh
│   ├── i18n
│   └── protobuf
├── README.MD
├── resource
│   ├── public
│   │   ├── html
│   │   ├── plugin
│   │   └── resource
│   │       ├── css
│   │       ├── image
│   │       └── js
│   └── template
└── utility

GoFrame框架有独特的项目工程结构,工程目录结构介绍具体请参考: 工程目录设计

运行项目

项目模板可以执行以下命令运行:
cd demo && gf run main.go
其中的 gf run 是框架开发工具的动态编译命令,也可以替换为 go run 命令。
执行后,终端输出:

root@ecs-1b41 ~/p/gomall# gf run main.go 
build: main.go
go build -o ./main  main.go
./main 
build running pid: 697781
2025-02-10 18:58:49.644 [INFO] pid[697781]: http server started listening on [:8000]
2025-02-10 18:58:49.645 [INFO] {bdf177b3a6d32218af28a01de3e2cc9f} swagger ui is serving at address: http://127.0.0.1:8000/swagger/
2025-02-10 18:58:49.645 [INFO] {bdf177b3a6d32218af28a01de3e2cc9f} openapi specification is serving at address: http://127.0.0.1:8000/api.json

  ADDRESS | METHOD |   ROUTE    |                        HANDLER                         |           MIDDLEWARE             
----------|--------|------------|--------------------------------------------------------|----------------------------------
  :8000   | ALL    | /api.json  | github.com/gogf/gf/v2/net/ghttp.(*Server).openapiSpec  |                                  
----------|--------|------------|--------------------------------------------------------|----------------------------------
  :8000   | GET    | /hello     | gomall/internal/controller/hello.(*ControllerV1).Hello | ghttp.MiddlewareHandlerResponse  
----------|--------|------------|--------------------------------------------------------|----------------------------------
  :8000   | ALL    | /swagger/* | github.com/gogf/gf/v2/net/ghttp.(*Server).swaggerUI    | HOOK_BEFORE_SERVE                
----------|--------|------------|--------------------------------------------------------|----------------------------------

默认情况下项目会设置Web服务端口为 8000、开启 OpenAPI 接口文档、展示 Swagger 接口文档页面,这些关键信息都会展示到终端。默认情况下,会打印所有的路由信息到终端,该项目模板只会添加一个仅供演示的路由: /hello。
默认路由: http://127.0.0.1:8000/hello

root@ecs-1b41 ~/project# curl http://127.0.0.1:8000/hello
Hello World!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值