This is a creation in
Article, where the information may have evolved or changed.
The main function of this program is to compare files under the A folder with the files in B directory, if found to overwrite the corresponding directory B.
Usage: Merge a directory B directory
Merge.go
PackageMainImport("Flag" "FMT" "OS" "Path/filepath" "Strings" "Time" "Github.com/unknwon/com")Const(isdirectory =IotaIsregular Issymlink)typeSysfilestruct{FTypeintFNamestringFLinkstringFsizeInt64Fmtime time. Time Fperm OS. FileMode}typeFstruct{Files []*sysfile}func(self *f) visit (Pathstring, F OS. FileInfo, err Error) error {iff = =Nil{returnERR}varTpint ifF.isdir () {tp = isdirectory}Else if(F.mode () & OS. Modesymlink) >0{TP = Issymlink}Else{tp = isregular} inofile: = &sysfile{Fname:path, FTYPE:TP, Fperm:f.mode (), Fmtime:f.modtime (), Fsize:f.size (),} self.files =Append(Self.files, Inofile)return Nil}funcMain () {flag. Parse () SourceDir: = flag. Arg(0) Decdir: = flag. Arg(1) Source: = f{files: Make([]*sysfile,0),} ERR: = FilePath. Walk (SourceDir,func(Pathstring, F OS. FileInfo, err Error) error {returnSource.visit (path, F, err)})ifErr! =Nil{FMT. Printf ("filepath. Walk () returned%v\n ", err)} Dec: = f{Files: Make([]*sysfile,0),} err = filepath. Walk (Decdir,func(Pathstring, F OS. FileInfo, err Error) error {returnDec.visit (path, F, err)})ifErr! =Nil{FMT. Printf ("filepath. Walk () returned%v\n ", err)} for_, V: =RangeSource.files {ifCom. Isfile (v.fname) = =true{tmp1: = strings. Split (V.fname,"\\") SourceName: = tmp1[Len(TMP1)-1] for_, R: =RangeDec.files {ifCom. Isfile (r.fname) = =true{tmp2: = strings. Split (R.fname,"\\") Decname: = tmp2[Len(TMP2)-1]ifSourceName = = Decname {fmt. Printf ("The same file:%s\n", SourceName) com. Copy (V.fname, R.fname)}}}}}