Mix Go 是一个基于 Go 进行快速开发的完整系统,类似前端的 Vue CLI,提供:
- 通过
mix-go/mixcli实现的交互式项目脚手架:- 可以生成
cli,api,web,grpc多种项目代码 - 生成的代码开箱即用
- 可选择是否需要
.env环境配置 - 可选择是否需要
.yml,.json,.toml等独立配置 - 可选择使用
gorm,xorm的数据库 - 可选择使用
logrus,zap的日志库
- 可以生成
- 通过
mix-go/xcli实现的命令行原型开发。 - 基于
mix-go/xdi的 DI, IoC 容器。
Github | Gitee
快速开始
安装
go get github.com/mix-go/mixcli
创建项目
$ mixcli new hello
Use the arrow keys to navigate: ↓ ↑ → ←
? Select project type:
▸ CLI
API
Web (contains the websocket)
gRPC
技术交流
知乎:https://www.zhihu.com/people/onanying
微博:http://weibo.com/onanying
官方QQ群:284806582, 825122875,敲门暗号:goer
编写一个 CLI 程序
首先我们使用 mixcli 命令创建一个项目骨架:
$ mixcli new hello
Use the arrow keys to navigate: ↓ ↑ → ←
? Select project type:
▸ CLI
API
Web (contains the websocket)
gRPC
生成骨架目录结构如下:
.
├── README.md
├── bin
├── commands
├── conf
├── configor
├── di
├── dotenv
├── go.mod
├── go.sum
├── logs
└── main.go
mian.go 文件:
xcli.AddCommand方法传入的commands.Commands定义了全部的命令
package main
import (
"github.com/mix-go/cli-skeleton/commands"
_ "github.com/mix-go/cli-skeleton/configor"
_ "github.com/mix-go/cli-skeleton/di"
_ "github.com/mix-go/cli-skeleton/dotenv"
"github.com/mix-go/dotenv"
"github.com/mix-go/xcli"
)
func main() {
xcli.SetName("app").
SetVersion("0.0.0-alpha").
SetDebug(dotenv.Getenv("APP_DEBUG").Bool(false))
xcli.AddCommand(commands.Commands...).Run()
}
commands/main.go 文件:
我们可以在这里自定义命令,查看更多
RunI定义了hello命令执行的接口,也可以使用Run设定一个匿名函数
package commands
import (
"github.com/mix-go/xcli"
)
var Commands = []*xcli.Command{
{
Name: "hello",
Short: "\tEcho demo",
Options: []*xcli.Option{
{
Names: []string{
"n", "name"},
Usage: "Your name",
},
{
Names: []string{
"say"},
Usage: "\tSay ...",
},
},
RunI: &HelloCommand{
},
},
}
commands/hello.go 文件:
业务代码写在 HelloCommand 结构体的 main 方法中
- 代码中可以使用
flag获取命令行参数,查看更多
package commands
import (
"fmt"
"github.com/mix-go/xcli/flag"
)
type HelloCommand struct {
}
func (t *HelloCommand) Main() {
name := flag.Match("n", "name").String("OpenMix")
say := flag.Match("say").String("Hello, World!")
fmt.Printf("%s: %s\n", name, say)
}
接下来我们编译上面的程序:
- linux & macOS
go build -o bin/go_build_main_go main.go
- win
go build -o bin/go_build_main_go.exe main.go
查看全部命令的帮助信息:
$ cd bin
$ ./go_build_main_go
Usage: ./go_build_main_go [OPTIONS] COMMAND [opt...]
Global Options:
-h, --help Print usage
-v, --version Print version information
Commands:
hello Echo demo
Run './go_build_main_go COMMAND --help' for more information on a command.
Developed with Mix Go framework. (openmix.org/mix-go)
查看上面编写的 hello 命令的帮助信息:
$ ./go_build_main_go hello --help
Usage: ./go_build_main_go hello [opt...]
Command Options:
-n, --name Your name
--say Say ...
Developed with Mix Go framework. (openmix.org/mix-go)
执行 hello 命令,并传入两个参数:
$ ./go_build_main_go hello --name=liujian --say=hello
liujian: hello
编写一个 Worker Pool 队列消费
队列消费是高并发系统中最常用的异步处理模型,通常我们是编写一个 CLI 命令行程序在后台执行 Redis、RabbitMQ 等 MQ 的队列消费,并将处理结果落地到 mysql 等数据库中,由于这类需求的标准化比较容易,因此我们开发了 mix-go/xwp 库来处理这类需求,基本上大部分异步处理类需求都可使用。
新建 commands/workerpool.go 文件:
workerpool.NewDispatcher(jobQueue, 15, NewWorker)创建了一个调度器NewWorker负责初始化执行任务的工作协程- 任务数据会在
worker.Do方法中触发,我们只需要将我们的业务逻辑写到该方法中即可 - 当程序接收到进程退出信号时,调度器能平滑控制所有的 Worker 在执行完队列里全部的任务后再退出调度,保证数据的完整性
package commands
import (
"context"
"fmt"
"github.com/mix-go/cli-skeleton/di"
"github.com/mix-go/xwp"
"os"
"os/signal"
"strings"
"syscall"
"time"
)
type worker struct {
xwp.WorkerTrait
}
func (t *worker) Do(data interface{
}) {
defer func() {
if err := recover(); err != nil {
logger := di.Logrus()
logger.Error(err)
}
}()
// 执行业务处理
// ...
// 将处理结果落地到数据库
// ...
}
func NewWorker() xwp.Worker {
return &worker{
}
}
type WorkerPoolDaemonCommand struct {
}
func (t *WorkerPoolDaemonCommand) Main() {
redis := globals.Redis()
jobQueue := make(chan interface{
}, 50)
d := xwp.NewDispatcher(jobQueue, 15, NewWorker)
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-ch
d.Stop()
}()
go func() {
for {
res, err := redis.BRPop(context.Background(), 3*time.Second, "foo").Result()
if err != nil {
if strings.Contains(err.Error(), "redis: nil") {
continue
}
fmt.Println(fmt.Sprintf("Redis Error: %s", err))
d.Stop();
return
}
// brPop命令最后一个键才是值
jobQueue <- res[1]
}
}()
d.Run() // 阻塞代码,直到任务全部执行完成并且全部 Worker 停止
}
接下来只需要把这个命令通过 xcli.AddCommand 注册到 CLI 中即可。
编写一个 API 服务
首先我们使用 mixcli 命令创建一个项目骨架:
$ mixcli new hello
Use the arrow keys to navigate: ↓ ↑ → ←
? Select project type:
CLI
▸ API
Web (contains the websocket)
gRPC
生成骨架目录结构如下:
.
├── README.md
├── bin
├── commands
├── conf
├── configor
├── controllers
├── di
├── dotenv
├── go.mod
├── go.sum
├── main.go
├── middleware
├── routes
└── runtime
mian.go 文件:
xcli.AddCommand方法传入的commands.Commands定义了全部的命令
package main
import (
"github.com/mix-go/api-skeleton/commands"
_ "github.com/mix-go/api-skeleton/configor"
_ "github.com/mix-go/api-skeleton/di"
_ "github.com/mix-go/api-skeleton/dotenv"
"github.com/mix-go/dotenv"
"github.com/mix-go/xcli"
)
func main() {
xcli.SetName("app").
SetVersion("0.0.0-alpha").
SetDebug(dotenv.Getenv("APP_DEBUG").

MixGo是一个基于Go的开发框架,提供交互式项目脚手架,支持生成CLI、API、Web服务和gRPC服务。它包含了命令行原型开发、DI/IoC容器、日志库和数据库支持。通过mix命令,用户可以轻松创建和配置项目,包括Worker Pool队列消费、WebSocket服务等。项目还包括详细的快速开始指南和技术交流平台。
最低0.47元/天 解锁文章
2075

被折叠的 条评论
为什么被折叠?



