ftp
ftp copied to clipboard
Walk example
Would someone be kind enough to share an example of how to use walk? Honestly i'm unable to work it out myself from the documentation (i'm still leaning go)
Thanks
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Would someone be kind enough to share an example of how to use walk? Honestly i'm unable to work it out myself from the documentation (i'm still leaning go)
Thanks
Yeah, I'm in the same boat. I looked at the tests for the Walker and then had a second look at the doco.
func main() {
host := "ftp.something.host:21"
folder := "/some_folder"
user := "gonzo"
passwd := "secret"
c, err := ftp.Dial(host, ftp.DialWithTimeout(5*time.Second))
if err != nil {
log.Fatal(err)
}
err = c.Login(user, passwd)
if err != nil {
log.Fatal(err)
}
w := c.Walk(folder)
ok := w.Next() // Get next (first) entry in the tree
for ok == true {
e := w.Stat() // Get the current entry
fmt.Println(e.Type, e.Name, w.Path())
ok = w.Next() // Get next
}
if err := c.Quit(); err != nil {
log.Fatal(err)
}
}
It's pretty similar to other libraries :)
package main
import (
"fmt"
"io"
"log"
"time"
"github.com/jlaffaye/ftp"
)
func main() {
conn, err := ftp.Dial("your.host:21", ftp.DialWithTimeout(5*time.Second))
if err != nil {
log.Fatal(err)
}
if err = conn.Login("username", "password"); err != nil {
log.Fatal(err)
}
walker := conn.Walk("/your/path")
for walker.Next() {
// access current full path
fmt.Println(walker.Path())
// access entry (file or folder)
fmt.Println(walker.Stat().Name)
}
if walker.Err() != nil && walker.Err() != io.EOF {
log.Fatal(err)
}
if err = conn.Quit(); err != nil {
log.Fatal(err)
}
}