Not right now. You do it by opening the source, creating the
destination, and calling io.Copy(dest, src). There was a CL for adding
a Copy function to io/ioutil a while back, but it must've been
abandoned.
> move
os.Rename
> or delete a file in Go.
os.Remove
- Evan
(*File).Readdirnames and (*File).Readdir ... a *File and *FileInfo can be
directories or files.
Here is a simple version of Copy (for files only).
func CopyFile(src, dest) (err os.Error) {
read, written int = 0
// check source file exists.
sStat, err = os.Stat(src) {
if err != nil {
return
}
if sStat.IsDirectory() {
return os.NewError(fmt.Sprintf("%s is a directory, can't copy.", sStat.Name)
}
s, err = os.Open(file, os.O_RDONLY, 0644)
if err != nil {
return
}
buffer []bytes
_, e := os.Stat(d)
if e != nil {
d, err = os.Open(dest, os.O_CREATE|os.O_TRUNC, 0666)
if err != nile {
return
}
readsize, err = s.Read(buffer)
if err != nil {
return
}
written, err = d.Write(buffer)
if err != nil {
return
}
// makes sure that # of bytes was written/read correctly.
if written < read {
return os.NewError(fmt.Sprintf("Not enough bytes written to %s", d.Name))
}
if read < written {
return os.NewError(fmt.Sprintf("Wrote more bytes than read to %s", d.Name))
}
return
}
For directory's, just cycle on the files from Readdirs or Readdirnames,
FileInfo does have a IsDirectory method as part of it's interface you can test
against and add a touch of recursion for walking the tree. Use os.Mkdir and
os.Mkdirall to create the paths the fils needs. Checking os.Stat will work
for checking existance.
Please note this was just submitted today:
http://code.google.com/p/go/source/detail?r=6c96d1022285
That change, changes how os.Open works. (usage above coincides with
os.OpenFile)
Mike
> On Apr 5, 5:29 pm, Evan Shaw <chicken...@gmail.com> wrote:
> > On Tue, Apr 5, 2011 at 5:24 PM, Nick Sarten <gen.bat...@gmail.com> wrote:
> > > I was just curious as to whether there's any way to copy,
> >
> > Not right now. You do it by opening the source, creating the
> > destination, and calling io.Copy(dest, src). There was a CL for adding
> > a Copy function to io/ioutil a while back, but it must've been
> > abandoned.
> >
> > > move
> >
> > os.Rename
> >
> > > or delete a file in Go.
> >
> > os.Remove
> >
> > - Evan
--
There are more things in heaven and earth than any place else.
import "os"
import "io"
func CopyFile(src, dst string) (int64, os.Error) {
sf, err := os.Open(src, os.O_RDONLY, 0)
if err != nil {
return 0, err
}
df, err := os.Open(dst, os.O_TRUNC|os.O_CREAT, 0644)
if err != nil {
return 0, err
}
return io.Copy(df, sf)
>> On Apr 5, 5:29 pm, Evan Shaw <chick...@gmail.com> wrote:
> It's usually good to close files. For example,
>
Grr, knew I forgot something.
Mike
--
I suppose one could claim that an undocumented feature has no
semantics. :-(
-- Larry Wall in <1997102900...@wall.org>
-rob
] With Go I've found that I can compile it on my dev
] system, and from there it will run on any other Linux system without the
] need of any additional libraries. I've not tried this using cgo yet...
This works just fine. In fact I did exactly this the other day to
demonstrate the same point, you don't need anything else installed,
runtimes or frameworks or whatever to just run the program. Of course
if your cgo extension uses a shared library, that shared library has
to be present as well.
Cheers,
-w
--
William Waites <mailto:w...@styx.org>
http://river.styx.org/ww/ <sip:w...@styx.org>
F4B3 39BF E775 CF42 0BAB 3DF0 BE40 A6DF B06F FD45
] Under Linux, use CDE[http://www.stanford.edu/~pgbovine/cde.html] for your
] packaging problems.
Merci Pascal, mais je n'ai aucune problème avec la
gestion des paquets...
OK, this is the second discussion today that has made me think I'm on
mars. At what point did it get to be unexpected behavior to give
someone a program and have it, so to speak, "run"? :-)
ron
] OK, this is the second discussion today that has made me think I'm on
] mars. At what point did it get to be unexpected behavior to give
] someone a program and have it, so to speak, "run"? :-)
Greetings earthling.
There were worries expressed to me about having to install "another
runtime" and having to manage "extra dependencies" etc.. Like if I
had written something in Ruby in a Python shop the question would
have been the same. I just demonstrated for them that with Go this
wasn't a concern.
When `dependency hell` became popular venacular.
--
Documentation:
Instructions translated from Swedish by Japanese for English
speaking persons.