Working with FTP protocol in Golang

综合编程 2017-04-03

One of the benefits of usingGolang is its http
package that provides an easy way to build robust REST APIs.

Unfortunately, it does not provide out of the box away to build FTP server or connect to FTP server.

In this article, we will explore that by demonstrating the usage of two third party packages that allow that.

Connecting to FTP server

The most robust and broadly used package that provides an FTP client build by Julien


go get -u


You can connect the targeted FTP server as it’s shown in the following code snippet:

client, err := fto.Dial("localhost:21")
if err != nil {
  return err

if err := client.Login("root", "password"); err != nil {
  return err

The following code snippet lists, download and delete all files that matches the wild card.

entries, _ := client.List(wildcard)

for _, entry := range entries {
  name := entry.Name
  reader, err := client.Retr(name)
  if err != nil {

For more advanced use cases, you can read the documentation

Building an FTP Server

It’s very easy to build an FTP Server by using goftp/server
package that provides a framework for building FTP server with any kind of data store (file system, AWS3 and so on).


go get


In order to run the server, you have to develop or use a driver that implements an interface that acts as bridge between the FTP protocol and your desired backing store:

type Driver interface {
    Stat(string) (FileInfo, error)
    ChangeDir(string) error
    ListDir(string, func(FileInfo) error) error
    DeleteDir(string) error
    DeleteFile(string) error
    Rename(string, string) error
    MakeDir(string) error
    GetFile(string, int64) (int64, io.ReadCloser, error)
    PutFile(string, io.Reader, bool) (int64, error)

Presently, the following drivers are available:

Let’s see how we can use the file system driver to build our own ftp server. We should install the package:

go get

Then we have to initialise and start the ftp server with the following code snippet:

factory := &filedriver.FileDriverFactory{
  RootPath: "path_to_directory_that_will_store_all_files",
  Perm: server.NewSimplePerm("root", "root")

opts := &server.ServerOpts{
  Factory: factory,
  Port: 2001,
  Hostname: "",
server  := server.NewServer(opts)

The RootPath
defines where the files will be stored, while the Perm
field defines how the user are going to be authenticated. Presently, the package support single user authentication by using SimplePerm
or you can use more advanced LevelDB

It’s so easy, right?


Creating HTTP Server in Go In this post, we will create a simple HTTP server in Go following some simple steps. Before starting, let me give you a brief introduction about Go. ...
gobox中的simplecache和levelcache 今天来说下gobox中的simplecache和levelcache simplecache simplecache提供了一个简单的内存kv 用法示例 package mainimport ( "" "
gosearch – awesome-go package explorer gosearch: The awesome package explorer for Golang gosearch turns the awesome-go into a CLI command that you can search for packages offline ...
Go 标准库介绍①string strings 包实现了用于操作字符串的简单函数,包括 string 导出函数和 Reader, Replacer 两个结构体。 strings 常用导出函数 判断字符串与子串关系 func EqualFold(s, t string) bool // 判断两个utf-...
封装golang websocket websocket出来好久了,一直没有动手去玩玩,今天抽了点时间写了一个golang的例子,下面简单记录一下。 协议 websocket是个二进制协议,需要先通过Http协议进行握手,从而协商完成从Http协议向websocket协议的转换。一旦握手结束,当前的TCP连接后续将采用二进制w...