One minute
Go标准项目模板生成脚本
为了项目的可维护性, 不至于每个项目都一套目录规范,
打算使用 https://github.com/golang-standards/project-layout 的结构标准
中文参考: https://github.com/golang-standards/project-layout/blob/master/README_zh.md
写了一个生成脚本
#结构
/yourpath/goinit/goinit.sh
/yourpath/goinit/main.go
goinit.sh
#!/bin/bash
PWD=$(pwd)
cd $PWD
if [[ "$1" = "" ]]; then
echo -ne "Please Input The Golang Porject Name: "
read project
if [ "$project" = "" ]; then
echo -e "demo";
PRO_NAME=demo;
else
PRO_NAME=$project;
fi
else
PRO_NAME=$1;
fi
#创建目录
echo "Init $PRO_NAME Directory …"
mkdir -p $PRO_NAME/api
mkdir -p $PRO_NAME/assets
mkdir -p $PRO_NAME/build
mkdir -p $PRO_NAME/cmd
mkdir -p $PRO_NAME/configs
mkdir -p $PRO_NAME/deployments
mkdir -p $PRO_NAME/docs
mkdir -p $PRO_NAME/examples
mkdir -p $PRO_NAME/githooks
mkdir -p $PRO_NAME/init
mkdir -p $PRO_NAME/internal
mkdir -p $PRO_NAME/pkg
mkdir -p $PRO_NAME/scripts
mkdir -p $PRO_NAME/test
mkdir -p $PRO_NAME/third_patry
mkdir -p $PRO_NAME/tools
mkdir -p $PRO_NAME/vendor
mkdir -p $PRO_NAME/web
mkdir -p $PRO_NAME/website
cd $PRO_NAME
go mod init $PRO_NAME
go mod vendor
# 我的项目目录 配置等
touch $PRO_NAME/configs/config.toml
mkdir -p $PRO_NAME/web/static
mkdir -p $PRO_NAME/web/templates
mkdir -p $PRO_NAME/web/spa
mkdir -p $PRO_NAME/internal/controllers
mkdir -p $PRO_NAME/internal/models
mkdir -p $PRO_NAME/internal/services
mkdir -p $PRO_NAME/internal/utils
# 其他
echo "Copy main.go to here"
cp /yourpath/goinit/main.go .
cd ..
cp /yourpath/goinit/.gitignore .
cp /yourpath/goinit/README_zh.md .
#gofmt main.go
echo "Init $PRO_NAME Success!"
main.go
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello World")
}
.zshrc里添加别名
alias goinit="~/Dropbox/mac/goinit/goinit.sh"
source ~/.zshrc
这样任意目录里执行 goinit即可生成项目结构
goinit
Please Input The Golang Porject Name: wechat
Init wechat Directory …
go: creating new go.mod: module wechat
Copy main.go to here
Init wechat Success!
cd wechat