This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("Archive/tar" "Compress/gzip" "FMT" "io" "io/ioutil" "OS" "strings") func main () {FMT. PRINTLN (gzip (". \", "1.tar.gz"))}func Gzip (filepath, filename string) error {File, err: = OS. Create (filename) if err! = Nil {return err}defer file.close () GW: = gzip. Newwriter (File) defer GW. Close () TW: = tar. Newwriter (GW) defer TW. Close () return walk (filepath, TW)}func Walk (path string, TW *tar. Writer) Error {Path = strings. Replace (path, "\ \", "/",-1) info, err: = Ioutil. ReadDir (PATH) if err! = Nil {return err}if!strings. Hassuffix (Path, "/") {Path = path + "/"}index: = Strings. Index (Path, "/") List: = Strings. Join (Strings. Split (Path, "/") [Index:], "/") for _, V: = Range Info {if V.isdir () {head: = tar. Header{name:list + v.name (), Typeflag:tar. Typedir, Modtime:v.modtime ()}tw. Writeheader (&head) Walk (Path+v.name (), TW) continue}f, err: = OS. Open (path + v.name ()) if err! = Nil {fmt. Println ("Open file%s failed.", err) Continue}head: = Tar. Header{name:list + v.name (), Size:v.size (), Mode:int64 (V.mode ()),Modtime:v.modtime ()}tw. Writeheader (&head) io. Copy (TW, F) f.close ()}return Nil}