1. 程式人生 > >Golang工程自動建立指令碼

Golang工程自動建立指令碼

1.Windows 版本 gogo.bat

參考Linux版本修改而來

rem 參考程式原文:http://www.cnblogs.com/ficow/p/6553344.html
rem Alex hu
rem 2017-07-26

rem 建立目錄
mkdir %1
cd %1
mkdir pkg
mkdir bin
mkdir src
mkdir src\config
mkdir src\%1

rem 建立config.go
@echo package config>> src\config\config.go
@echo func LoadConfig() { >> src\config\config.go
@echo
} >> src\config\config.go rem 建立main.go @echo package main>> src\%1\main.go @echo import ( >> src\%1\main.go @echo "config" >> src\%1\main.go @echo "fmt" >> src\%1\main.go @echo ) >> src\%1\main.go @echo func main() { >> src\%1\main.go @echo config.LoadConfig
() >> src\%1\main.go @echo fmt.Println("Message From %1.exe: Hello World!") >> src\%1\main.go @echo } >> src\%1\main.go rem 建立install.bat @echo set curdir=%%cd%%>>install.bat @echo set oldgopath=%%GOPATH%%>>install.bat @echo set GOPATH=%%curdir%%>>install.bat @echo
gofmt -w src>> install.bat @echo go install %1 >> install.bat @echo set GOPATH=%%oldgopath%%>> install.bat @echo echo finished >> install.bat

2.Mac/Linux版 gogo.sh

原文地址:http://www.cnblogs.com/ficow/p/6553344.html

#!/bin/bash
#————————————–
# Module : mk_go_pro.sh
# Author : Blair Zhong
# Created : 2013.07.23
# Modify :
# Version : 1.0
# Useage : ./mk_go_pro.sh
# ./mk_go_pro.sh porject_name
# Description: 建立一個go可編譯的工程
#————————————–
# 根據 Go語言學習園地部落格的帖子編寫,如有侵權請聯絡本人
# http://blog.studygolang.com/2012/12/go專案的目錄結構/
# 預設情況下執行本程式,會生成如下目錄和檔案
# test
# ├── bin
# ├── install.sh
# ├── pkg
# └── src
# ├── config
# │   └── config.go
# └── test
# └── main.go
#
# 5 directories, 3 files
#
# 其中:
# 1, install.sh為安裝檔案,
# 2, config.go為test專案的配置檔案
# 3, main.go這個你懂的
# 生成完畢之後執行進入test目錄,執行install.sh會生成如下檔案和目錄
# ├── bin
# │   └── test
# ├── install.sh
# ├── pkg
# │   └── darwin_amd64
# │   └── config.a
# └── src
# ├── config
# │   └── config.go
# └── test
# └── main.go
# 6 directories, 5 files
#
# 多了兩個檔案
# 1, bin目錄下的test,這個是可執行穩健
# 2, pkg/darwin_amd64下的config.a,這個是config編譯後產生的檔案
#
# enjoy it!

PWD=$(pwd)
cd $PWD

if [[ "$1" = "" ]]; then
echo "Useage: ./mk_go_pro.sh porject_name"
echo -ne "Please input the Porject Name[test]"
read Answer
if [ "$Answer" = "" ]; then
echo -e "test";
PRO_NAME=test;
else
PRO_NAME=$Answer;
fi
else
PRO_NAME=$1;
fi
#建立目錄
echo "Init Directory …"
mkdir -p $PRO_NAME/bin
mkdir -p $PRO_NAME/pkg
mkdir -p $PRO_NAME/src/config
mkdir -p $PRO_NAME/src/$PRO_NAME

#建立install檔案
echo "Create install/install.sh …"
cd $PRO_NAME
echo '#!/bin/bash' > install.sh
echo 'if [ ! -f install.sh ]; then' >> install.sh
echo "echo 'install must be run within its container folder' 1>&2" >> install.sh
echo "exit 1" >> install.sh
echo "fi" >> install.sh
echo >> install.sh
echo "CURDIR=\`pwd\`" >> install.sh
echo "OLDGOPATH=\"\$GOPATH\"" >> install.sh
echo "export GOPATH=\"\$CURDIR\"" >> install.sh
echo >> install.sh
echo "gofmt -w src" >> install.sh
echo "go install $PRO_NAME" >> install.sh
echo "export GOPATH=\"\$OLDGOPATH\"" >> install.sh
echo >> install.sh
echo "echo 'finished'" >>install.sh
chmod +x install.sh

#建立config.go檔案
echo "Create src/config/config.go …"
cd src/config
echo package config > config.go
echo >> config.go
echo func LoadConfig\(\) { >> config.go
echo >> config.go
echo } >> config.go

#建立main.go
echo "Create src/$PRO_NAME/main.go …"
cd ../$PRO_NAME/
echo "package main" > main.go
echo >> main.go
echo "import (" >> main.go
echo " \"config\"" >> main.go
echo " \"fmt\"" >> main.go
echo ")" >> main.go
echo >> main.go
echo "func main() {" >> main.go
echo " config.LoadConfig()" >> main.go
echo " fmt.Println(\"Hello $PRO_NAME!\")" >> main.go
echo "}" >> main.go
echo "All Done!"