Copy a folder in go

Clash Royale CLAN TAG#URR8PPP
Copy a folder in go
Is there an easy way to copy a directory in go?
I have the following function:
err = CopyDir("sourceFolder","destinationFolder")
Nothing so far has worked, including libraries such as github.com/cf-guardian/guardian/kernel/fileutils
One important thing to note is that I need to preserve directory structure, including the sourceFolder itself, not simply copy all contents of the folder.
CopyDir
On which system should this work? You could use a system program for example
cp, which copies your folder. You can call that program via the os/exec package.– apxp
Aug 10 at 5:13
cp
os/exec
2 Answers
2
An easy and lazy way would be to use the copy command of your system. This example just works on systems with the command cp (so not for Windows)
func CopyDir(src, dst string) error
cmd := exec.Command("cp", "a")
log.Printf("Running cp -a")
return cmd.Run()
To preserve of the directories is not so easy to implement, so to use the system program for that is ok. But that solution does not work on all systems, because it needs an external program. I would just use it for internal purposes.
Did not know you could use system commands in go. The more you know, works thanks!
– P A S H
Aug 10 at 9:02
This package seems to do exactly what you want to do, give it a try.
From the readme:
err := Copy("your/source/directory", "your/destination/directory")
In go the libraries a called packages ;-)
– apxp
Aug 10 at 5:14
Right, sorry about that, edited :p
– Ullaakut
Aug 10 at 5:35
@apxp there are libraries consisting of multiple packages ;)
– Havelock
Aug 10 at 5:54
@Havelock good point
– apxp
Aug 10 at 6:02
Please dont post just link, post a little exampe and description.
– Зелёный
Aug 10 at 6:50
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Can you show us what your
CopyDirfunction does? Then we might be able to help you.– Ullaakut
Aug 10 at 5:08