golang用zip打包文件简单例子

代码如下所示:

package main
 
import (
    "archive/zip"
    "io"
    "fmt"
    "os"
)
 
func main() {
    files := []string{"aa", "1.txt"}
    output := "out.zip"
 
    newZipFile, err := os.Create(output)
    errOut(err)
 
    defer newZipFile.Close()
 
    zipWriter := zip.NewWriter(newZipFile)
    defer zipWriter.Close()
 
    for _, file := range files {
        fileToZip, err := os.Open(file)
        errOut(err)
 
        info, err := fileToZip.Stat()
        errOut(err)
 
        header, err := zip.FileInfoHeader(info)
        errOut(err)
 
        header.Name = file
        header.Method = zip.Deflate
 
        writer, err := zipWriter.CreateHeader(header)
        errOut(err)
 
        _, err = io.Copy(writer, fileToZip)
        errOut(err)
    }
 
    fmt.Println("zip done!")
}
 
func errOut(err error) {
    if err != nil {
        panic(err)
    }
}

centos5.x/redhat5.x安装GO语言

现在GO语言比较火, 自己也兴趣学习。下面是centos5.x安装GO语言过程

下载安装包

$ wget https://storage.googleapis.com/golang/go1.2.2.linux-amd64.tar.gz

解压安装

$ tar xzvf go1.2.2.linux-amd64.tar.gz -C /usr/local/

在你的PATH变量加上:/usr/local/go/bin环境变量
如下面的示例
vim ~/.bash_profile
PATH=$PATH:/usr/local/php/bin:/usr/local/mysql/bin:/usr/local/go/bin
继续阅读