Golang bash脚本自动创建一个go工程
原作者的代码里面,存在智能引号问题,所以他的代码并不能正常运行;
这个是我微调后的版本。
代码如下:
1 #!/bin/bash 2 #————————————– 3 # Module : mk_go_pro.sh 4 # Author : Blair Zhong 5 # Created : 2013.07.23 6 # Modify : 7 # Version : 1.0 8 # Useage : ./mk_go_pro.sh 9 # ./mk_go_pro.sh porject_name 10 # Description: 创建一个go可编译的工程 11 #————————————– 12 # 根据 Go语言学习园地博客的帖子编写,如有侵权请联系本人 13 # http://blog.studygolang.com/2012/12/go项目的目录结构/ 14 # 默认情况下运行本程序,会生成如下目录和文件 15 # test 16 # ├── bin 17 # ├── install.sh 18 # ├── pkg 19 # └── src 20 # ├── config 21 # │ └── config.go 22 # └── test 23 # └── main.go 24 # 25 # 5 directories, 3 files 26 # 27 # 其中: 28 # 1, install.sh为安装文件, 29 # 2, config.go为test项目的配置文件 30 # 3, main.go这个你懂的 31 # 生成完毕之后运行进入test目录,运行install.sh会生成如下文件和目录 32 # ├── bin 33 # │ └── test 34 # ├── install.sh 35 # ├── pkg 36 # │ └── darwin_amd64 37 # │ └── config.a 38 # └── src 39 # ├── config 40 # │ └── config.go 41 # └── test 42 # └── main.go 43 # 6 directories, 5 files 44 # 45 # 多了两个文件 46 # 1, bin目录下的test,这个是可执行稳健 47 # 2, pkg/darwin_amd64下的config.a,这个是config编译后产生的文件 48 # 49 # enjoy it! 50 51 PWD=$(pwd) 52 cd $PWD 53 54 if [[ "$1" = "" ]]; then 55 echo "Useage: ./mk_go_pro.sh porject_name" 56 echo -ne "Please input the Porject Name[test]" 57 read Answer 58 if [ "$Answer" = "" ]; then 59 echo -e "test"; 60 PRO_NAME=test; 61 else 62 PRO_NAME=$Answer; 63 fi 64 else 65 PRO_NAME=$1; 66 fi 67 #创建目录 68 echo "Init Directory …" 69 mkdir -p $PRO_NAME/bin 70 mkdir -p $PRO_NAME/pkg 71 mkdir -p $PRO_NAME/src/config 72 mkdir -p $PRO_NAME/src/$PRO_NAME 73 74 #创建install文件 75 echo "Create install/install.sh …" 76 cd $PRO_NAME 77 echo '#!/bin/bash' > install.sh 78 echo 'if [ ! -f install.sh ]; then' >> install.sh 79 echo "echo 'install must be run within its container folder' 1>&2" >> install.sh 80 echo "exit 1" >> install.sh 81 echo "fi" >> install.sh 82 echo >> install.sh 83 echo "CURDIR=\`pwd\`" >> install.sh 84 echo "OLDGOPATH=\"\$GOPATH\"" >> install.sh 85 echo "export GOPATH=\"\$CURDIR\"" >> install.sh 86 echo >> install.sh 87 echo "gofmt -w src" >> install.sh 88 echo "go install $PRO_NAME" >> install.sh 89 echo "export GOPATH=\"\$OLDGOPATH\"" >> install.sh 90 echo >> install.sh 91 echo "echo 'finished'" >>install.sh 92 chmod +x install.sh 93 94 #创建config.go文件 95 echo "Create src/config/config.go …" 96 cd src/config 97 echo package config > config.go 98 echo >> config.go 99 echo func LoadConfig\(\) { >> config.go 100 echo >> config.go 101 echo } >> config.go 102 103 #创建main.go 104 echo "Create src/$PRO_NAME/main.go …" 105 cd ../$PRO_NAME/ 106 echo "package main" > main.go 107 echo >> main.go 108 echo "import (" >> main.go 109 echo " \"config\"" >> main.go 110 echo " \"fmt\"" >> main.go 111 echo ")" >> main.go 112 echo >> main.go 113 echo "func main() {" >> main.go 114 echo " config.LoadConfig()" >> main.go 115 echo " fmt.Println(\"Hello $PRO_NAME!\")" >> main.go 116 echo "}" >> main.go 117 echo "All Done!"
参考链接:http://blog.studygolang.com/2012/12/go%E9%A1%B9%E7%9B%AE%E7%9A%84%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84/ 作者的评论在13楼
Stay hungry,stay foolish.