Go project to compile Linux or Windows executable files

Compile into windows file

SET CGO_ENABLED=1
SET GOARCH=
SET GOOS=windows
go build

Compile to Linux file

SET CGO_ENABLED=0
SET GOARCH=amd64
SET GOOS=linux
go build

Static resource file packaging

The above packaging can only package go language files. If static resources or configuration files are used in the project, they will not be packaged, and an error will be reported after running the packaged file

Solution: Use go-bindata to package the configuration file into a binary go file

First install go-bindata

go get -u github.com/jteeuwen/go-bindata/...   # golang
go-bindata   #执行此命令验证安装成功与否

#如果命令执行失败,则
echo $GOPATH/bin   #查看gopath/bin目录是否有go-bindata可执行文件
cp $GOPATH/bin/go-bindata  /bin

go-bindata packages static files into go files

go-bindata -o=bindata/bindata.go -pkg=bindata conf/... crts/...

-o=bindata/bindata.go: The generated bindata.go file is packaged and placed in the bindata/ directory

-pkg=bindata: Bindata.go file generated by packaging, the package name is set to bindata

conf/... crts/...: all files in the folder to be packaged

View command parameters

go-bindata --help

Get static resources packaged by go-bindata in the go program

Method 1: Read the content of the static file packaged by go-bindata directly in the go program (cannot be modified in real time)

func main() {
    
    

	// 获取所有资源路径和文件名称
	assets := bindata.AssetNames()
	for _, asset := range assets {
    
    
		//获取配置文件内容,bytes中就是配置文件内容的字节数组
		bytes, err := bindata.Asset(asset)
		fmt.Println(bytes, err)
	}
	
}

Method 2: Unzip the configuration file to the same folder as the static resource path, and then read the configuration file through the go program (can be modified in real time)

func main() {
    
    
	// 释放资源文件
	releaseResources()
	// 加载配置文件(此处省略)
	config.LoadConfiguration()
	......
}
/**
  * @ Time:  2020/12/28 13:49
  * @ Author: qyz
  * @ Description: 释放bindata打包的静态资源
  * @ Param:
  *          : 
  * @ return:
  *          : 
 **/
func releaseResources() {
    
    
    // 要bindata打包前的静态文件路径
	confPath := "./conf"
	crtsPath := "./crts/mqtt"
	// 递归创建文件夹
	err := os.MkdirAll(confPath, os.ModePerm)
	if err != nil {
    
    
		log.Fatal("os.MkdirAll 创建静态资源文件出错:", err)
	}
	err = os.MkdirAll(crtsPath, os.ModePerm)
	if err != nil {
    
    
		log.Fatal("os.MkdirAll 创建静态资源文件出错:", err)
	}

	// 获取所有资源路径和文件名称
	filePaths := bindata.AssetNames()

	for _, filePath := range filePaths {
    
    

		isExist := IsFileOrDirectoryExist(filePath)

		if !isExist {
    
     // 如果不存在
			// 获取配置文件内容,bytes中就是配置文件内容的字节数组
			contentBytes, err := bindata.Asset(filePath)
			if err != nil {
    
    
				log.Fatal("bindata.Asset获取配置失败:", err)
			}
			writeFile(filePath, contentBytes)
		}

	}
}

/**
 * @ Time:  2020/12/28 10:53
 * @ Author: qyz
 * @ Description: 将bindata压缩的文件解压到当前目录
 * @ Param:
 *          :
 * @ return:
 *          :
**/
func writeFile(filePath string, contentBytes []byte) {
    
    

	file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, 0666)
	if err != nil {
    
    
		log.Fatal("os.OpenFile 出错:", err)
		return
	}
	defer file.Close()

	writer := bufio.NewWriter(file)

	if _, err := writer.Write(contentBytes); err != nil {
    
    
		log.Fatal("bufio.NewWriter().Write() 出错:", err)
	}

	if err := writer.Flush(); err != nil {
    
    
		log.Fatal("bufio.NewWriter().Flush() 出错:", err)
	}
}

/**
 * @ Time:  2020/12/28 10:46
 * @ Author: qyz
 * @ Description: 判断文件或文件夹是否存在
 * @ Param:
 *          :
 * @ return:
 *          :
**/
func IsFileOrDirectoryExist(filePath string) bool {
    
    
	_, err := os.Stat(filePath)
	return err == nil || os.IsExist(err)
}

If you want to learn more Go language grammar and common knowledge points of Go language at work, you can refer to my note source code https://github.com/qiuyunzhao/go_basis

Guess you like

Origin blog.csdn.net/QiuHaoqian/article/details/106780743