Goftp is a simple FTP library written in golang. The implementation is based on the RFC 959 - FILE TRANSFER PROTOCOL (FTP)
- active and passive data connection mode
Download the package and import it into your project.
import ftp "github.com/martinr92/goftp"
Connect to the remote server.
ftpClient, err := ftp.NewFtp("host.local:51000")
if err != nil {
panic(err)
}
defer ftpClient.Close()
By default, the client uses a passive data connection for file transfer. If you want to use a active connection, just set the following:
ftpClient.ActiveMode = true
ftpClient.ActiveModeIPv4 = "1.2.3.4"
Send user credentials.
if err = ftpClient.Login("username", "password"); err != nil {
panic(err)
}
Change the working directory.
if err = ftpClient.OpenDirectory("/some/folder/"); err != nil {
panic(err)
}
Upload a file.
if err = ftpClient.Upload("/local/path/file.txt", "file.txt"); err != nil {
panic(err)
}