1.hello goframe

107 阅读3分钟

hello goframe

mxy 2024-06-09

1.安装 goframe cli 工具

go install github.com/gogf/gf/cmd/gf/v2@latest

运行完成后命令行输入gf提示如下、表示安装成功

workspace\project
➜ gf
USAGE
    gf COMMAND [OPTION]

COMMAND
    up         upgrade GoFrame version/tool to latest one in current project
    env        show current Golang environment variables
    fix        auto fixing codes after upgrading to new GoFrame version
    run        running go codes with hot-compiled-like feature
    gen        automatically generate go files for dao/do/entity/pb/pbentity
    tpl        template parsing and building commands
    init       create and initialize an empty GoFrame project
    pack       packing any file/directory to a resource file, or a go file
    build      cross-building go project for lots of platforms
    docker     build docker image for current GoFrame project
    install    install gf binary to system (might need root/admin permission)
    version    show version information of current binary

OPTION
    -y, --yes       all yes for all command without prompt ask
    -v, --version   show version information of current binary
    -d, --debug     show internal detailed debugging information
    -h, --help      more information about this command

ADDITIONAL
    Use "gf COMMAND -h" for details about a command.

2.创建项目

自己找个文件夹打开命令行后输入以下命令来创建项目

gf init goframe_tutorial -u

goframe_tutorial为项目名称,可随意,-u为使用最新的模板

显示以下输出为成功

workspace\code\go
➜ gf init goframe_tutorial -u
initializing...
update goframe...
go: upgraded github.com/BurntSushi/toml v1.2.0 => v1.3.2
go: upgraded github.com/fatih/color v1.15.0 => v1.16.0
go: upgraded github.com/fsnotify/fsnotify v1.6.0 => v1.7.0
go: upgraded github.com/gogf/gf/v2 v2.5.2 => v2.7.1
go: upgraded github.com/gorilla/websocket v1.5.0 => v1.5.1
go: upgraded github.com/grokify/html-strip-tags-go v0.0.1 => v0.1.0
go: upgraded github.com/magiconair/properties v1.8.6 => v1.8.7
go: upgraded github.com/mattn/go-isatty v0.0.19 => v0.0.20
go: upgraded golang.org/x/net v0.12.0 => v0.24.0
go: upgraded golang.org/x/sys v0.10.0 => v0.19.0
go: upgraded golang.org/x/text v0.11.0 => v0.14.0
initialization done!
you can now run "cd goframe_tutorial && gf run main.go" to start your journey, enjoy!

然后用 vscode 打开该项目 你也可以使用其他编辑器

可以使用快捷命令打开,没安装命令的请手动打开

code .\goframe_tutorial

3.运行项目

gf run .\main.go

显示以下代表运行成功

➜ gf run .\main.go
build: .\main.go
go build -o ./\main.exe  .\main.go
./\main.exe
build running pid: 24772
2024-06-09 22:55:47.213 [INFO] pid[24772]: http server started listening on [:8000]
2024-06-09 22:55:47.213 [INFO] {10fc5852d35dd717d5202b49e1ffe80d} swagger ui is serving at address: http://127.0.0.1:8000/swagger/
2024-06-09 22:55:47.213 [INFO] {10fc5852d35dd717d5202b49e1ffe80d} 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     | goframe_tutorial/internal/controller/hello.(*ControllerV1).Hello | ghttp.MiddlewareHandlerResponse
----------|--------|------------|------------------------------------------------------------------|----------------------------------
  :8000   | ALL    | /swagger/* | github.com/gogf/gf/v2/net/ghttp.(*Server).swaggerUI              | HOOK_BEFORE_SERVE

----------|--------|------------|------------------------------------------------------------------|----------------------------------

4.编写hello goframe的接口

结束

  • api 设计
  • open api 文档
  • gf cli