写了个命令行的脚手架,感觉还挺好使

2022-05-18 17:28:13 +08:00
 PeterYang1996

gostage

gostage 是一个快速搭建常驻服务的命令行脚手架。

安装

go get github.com/PeterYangs/gostage

快速开始

编写一个每秒往一个文件中写入一行文本的服务

package main
import (
	"context"
	"fmt"
	"github.com/PeterYangs/gostage"
	"os"
	"time"
)
func main() {
	g := gostage.NewStage(context.Background())
	//绑定主服务逻辑
	g.StartFunc(func(st *gostage.Stage) error {
		//打开文件
		file, err := os.OpenFile("word.txt", os.O_CREATE|os.O_RDWR, 0644)
		if err != nil {
			return err
		}
		//计数
		index := 0
		defer file.Close()
		for {
			select {
			case <-st.GetCxt().Done():
				return nil
			default:
				//打印计数到终端
				fmt.Println(index)
				//每秒写入一行文本
				time.Sleep(1 * time.Second)
				file.Write([]byte("word\n"))
				index++
			}
		}
	})
	err := g.Run()
	if err != nil {
		fmt.Println(err)
	}
}

启动

go run stage.go 或 go run stage.go start

守护进程

go  go run stage.go start -d

停止

go run stage.go stop

帮助

go run stage.go help

地址: https://github.com/PeterYangs/gostage

878 次点击
所在节点    程序员
1 条回复
lesismal
2022-05-18 19:54:48 +08:00
读君一段 go ,似读一段 go

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/853750

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX