A FTP and FTPS client package for Go
go get -u github.com/dchang-dchang/ftp
Create a FTP connection
host, port := "ftp.hostname.com", 21
_ = s.Dial(fmt.Sprintf("%s:%d", host, port))
defer s.Quit()
Set timeouts, debug mode, and/or TLS config for implicit TLS
(Note: If a TLSConfig is set, both the command and data channel will be secured with the same config)
host, port := "ftp.hostname.com", 990
s := ftp.ServerConn{
Debug: true,
Timeout: 30*time.Second,
TLSConfig: &tls.Config{ServerName: host},
}
_ = s.Dial(fmt.Sprintf("%s:%d", host, port))
defer s.Quit()
Login
_ = s.Login(username, password)
List files
entries, _ := s.List("/")
for _, entry := range entries {
fmt.Println(entry)
}
Get a file
res, _ := s.Retr("/filename.txt")
reader := bufio.NewReader(res)
for {
line, err := reader.ReadString('\n')
if err == io.EOF {
break
} else if err != nil {
panic(err)
}
fmt.Println(line)
}