GoFramePHPLaravelJavaSpringBootPythonDjango
mod
新建go-hello项目,执行
go mod init go-hello
go.modgo.mod
module go-hello
require github.com/gogf/gf latest
GoFramelatestgo.modgo.sum
使用命令行
GoLand打开Terminal,在项目根目录下执行:
go get -u github.com/gogf/gf
该命令将会立即下载最新稳定版本的GoFrame框架。
使用GoFrame
我们将之前的hello.go修改如下:
package main
import (
"fmt"
"github.com/gogf/gf"
)
func main() {
fmt.Println("hello GF", gf.VERSION)
}
运行结果
方法二:gf-cli安装GoFrame
WIN10 下载执行文件 双击运行,选择安装位置GO/bin对应的数字
gf -v
#快速建空项目
gf init project
gf gen dao
gf gen model
#运行项目
gf run main.go
生成model
gf gen model ./app/model -c config/config.toml -p sys_ -t sys_users
命令说明
- ./app/model #在model生成的路径
- -c config/config.toml #在这个配置里找database数据库连接配置 需要写好mysql的配置信息
- -p sys_ #去除生成文件目录的sys前缀 如果不加这个参数就会按数据库名生成目录和文件名 如:sys_users
- -t sys_users #要生成model的数据表文件名
每个表一个目录,目录下生成三个文件
- 1. `数据表名.go` 自定义文件,开发者可以自由定义填充的代码文件,仅会生成一次,每一次模型生成不会覆盖。
- 2. `数据表名_entity.go` 表结构文件,根据数据表结构生成的结构体定义文件,包含字段注释。数据表在外部变更后,可使用gen命令重复生成更新该文件。
- 3. `数据表名_model.go` 表模型文件,为数据表提供了许多便捷的CURD操作方法,并可直接查询返回该表的结构体对象。数据表在外部变更后,可使用gen命令重复生成更新该文件。
GoFrame 的入门CURD的项目例子
下载代码
git clone https://github.com/gogf/gf-demos
创建数据库
create database gframeDemo default CHARSET utf8mb4 collate utf8mb4_unicode_ci;
use gframeDemo;
CREATE TABLE `user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户ID',
`passport` varchar(45) NOT NULL COMMENT '用户账号',
`password` varchar(45) NOT NULL COMMENT '用户密码',
`nickname` varchar(45) NOT NULL COMMENT '用户昵称',
`create_at` datetime DEFAULT NULL COMMENT '创建时间',
`update_at` datetime DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
创建配置文件
cp config/config.example.toml config/config.toml
vi config/config.toml:
# HTTP Server.
[server]
Address = ":8199"
ServerRoot = "public"
ServerAgent = "gf-demos"
LogPath = "/tmp/log/gf-demos/server"
NameToUriType = 2
RouteOverWrite = true
# Logger configurations.
[logger]
Path = "/tmp/log/gf-demos"
Level = "all"
Stdout = true
# Template view configurations.
[viewer]
Path = "template"
DefaultFile = "index.html"
Delimiters = ["${", "}"]
# Database configurations.
[database]
link = "mysql:root:123456@tcp(127.0.0.1:3306)/gframeDemo"
debug = true
# 数据库日志对象配置
[database.logger]
Path = "/tmp/log/gf-demos/sql"
Level = "all"
Stdout = true
# HTTP basic authentication passport/password for swagger api page.
[swagger]
user = "123"
pass = "123"
运行
项目根目录执行如下命令
go run main.go
说明
- gf-demos 提供的通用 curd 接口非常强大,可以实现对数据库任意表的 curd 操作。
- gf-demos 还演示了一个聊天室功能的实现, 应用了 websocket。
go mod tidy