Home > Backend Development > Golang > Using FTP in Go: A Complete Guide

Using FTP in Go: A Complete Guide

PHPz
Release: 2023-06-17 18:31:05
Original
3215 people have browsed it

With the rapid development of the Internet, File Transfer Protocol (FTP) has always been an important file transfer method. In Go language, using FTP to transfer files may be a need of many developers. However, maybe many people don't know how to use FTP in Go language. In this article, we will explore how to use FTP in Go language, from connecting to FTP server to file transfer, and how to handle errors and exceptions.

Create FTP connection

In the Go language, we can use the standard "net" package to connect to the FTP server and perform file transfer operations. First, we need to establish an FTP connection. Using the "net.Dial" function we can create a network connection to communicate with the FTP server. In this example we will use an FTP server.

package main

import (
    "fmt"
    "net"
)

func main() {
    conn, err := net.Dial("tcp", "ftp.mozilla.org:21")
    if err != nil {
        fmt.Println("Error connecting to server:", err)
        return
    }
    fmt.Println("Connected to server")
    conn.Close()
}
Copy after login

Use FTP client to log in to the server

Once the connection is established, we need to use FTP client to log in. Using the "NewConn" function from the "net/textproto" package, we can create a read-write connection to communicate with the FTP server. Next, we need to authenticate using the "Login" function.

package main

import (
    "fmt"
    "net"
    "net/textproto"
)

func main() {
    conn, err := net.Dial("tcp", "ftp.mozilla.org:21")
    if err != nil {
        fmt.Println("Error connecting to server:", err)
        return
    }
    defer conn.Close()
    fmt.Println("Connected to server")

    client := textproto.NewConn(conn)
    _, err = client.Cmd("USER anonymous")
    if err != nil {
        fmt.Println("Error sending user command:", err)
        return
    }
    _, err = client.Cmd("PASS password")
    if err != nil {
        fmt.Println("Error sending password command:", err)
        return
    }
    fmt.Println("Logged in as anonymous")
}
Copy after login

FTP client command

FTP client command sends a command to the FTP server in the form of a string. The following are common FTP client commands:

  • USER - the username to log in to the FTP server.
  • PASS - Password to log in to the FTP server.
  • PWD - Displays the current server directory.
  • CWD - Change the current server directory.
  • LIST - List the files and subdirectories of the current directory.
  • RETR - Download file from server.
  • STOR - Upload files to the server.
  • DELE - Delete a file on the server.
  • RNFR - Rename file source.
  • RNTO - Rename file target.

FTP Client File Upload and Download

Once the connection is established and logged in through the FTP client, we can transfer files. In Go language, transferring files using FTP client is very simple. We just need to use "RETR" command to download files or "STOR" command to upload files. We can use the "Copy" function in the "io" package to copy the file contents from the FTP server to a local file, or to copy the local file contents to the FTP server.

Let’s take a look at how to download files using an FTP client.

package main

import (
    "fmt"
    "io"
    "net"
    "net/textproto"
    "os"
)

func main() {
    conn, err := net.Dial("tcp", "ftp.mozilla.org:21")
    if err != nil {
        fmt.Println("Error connecting to server:", err)
        return
    }
    defer conn.Close()
    fmt.Println("Connected to server")

    client := textproto.NewConn(conn)
    _, err = client.Cmd("USER anonymous")
    if err != nil {
        fmt.Println("Error sending user command:", err)
        return
    }
    _, err = client.Cmd("PASS password")
    if err != nil {
        fmt.Println("Error sending password command:", err)
        return
    }
    fmt.Println("Logged in as anonymous")

    _, err = client.Cmd("TYPE I")
    if err != nil {
        fmt.Println("Error sending type command:", err)
        return
    }

    _, err = client.Cmd("PASV")
    if err != nil {
        fmt.Println("Error sending pasv command:", err)
        return
    }
    response, err := client.ReadLine()
    if err != nil {
        fmt.Println("Error reading pasv response:", err)
        return
    }
    fmt.Println(response)

    _, err = client.Cmd("RETR /pub/README.txt")
    if err != nil {
        fmt.Println("Error sending retr command:", err)
        return
    }

    response, err = client.ReadLine()
    if err != nil {
        fmt.Println("Error reading retr response:", err)
        return
    }
    fmt.Println(response)

    file, err := os.Create("README.txt")
    if err != nil {
        fmt.Println("Error creating file:", err)
        return
    }
    defer file.Close()

    _, err = io.Copy(file, conn)
    if err != nil {
        fmt.Println("Error copying file:", err)
        return
    }

    fmt.Println("Downloaded README.txt")
}
Copy after login

Let’s take a look at how to upload files using an FTP client.

package main

import (
    "fmt"
    "io"
    "net"
    "net/textproto"
    "os"
)

func main() {
    conn, err := net.Dial("tcp", "ftp.mozilla.org:21")
    if err != nil {
        fmt.Println("Error connecting to server:", err)
        return
    }
    defer conn.Close()
    fmt.Println("Connected to server")

    client := textproto.NewConn(conn)
    _, err = client.Cmd("USER anonymous")
    if err != nil {
        fmt.Println("Error sending user command:", err)
        return
    }
    _, err = client.Cmd("PASS password")
    if err != nil {
        fmt.Println("Error sending password command:", err)
        return
    }
    fmt.Println("Logged in as anonymous")

    _, err = client.Cmd("TYPE I")
    if err != nil {
        fmt.Println("Error sending type command:", err)
        return
    }

    _, err = client.Cmd("PASV")
    if err != nil {
        fmt.Println("Error sending pasv command:", err)
        return
    }
    response, err := client.ReadLine()
    if err != nil {
        fmt.Println("Error reading pasv response:", err)
        return
    }
    fmt.Println(response)

    _, err = client.Cmd("STOR /public_html/test.txt")
    if err != nil {
        fmt.Println("Error sending stor command:", err)
        return
    }

    file, err := os.Open("test.txt")
    if err != nil {
        fmt.Println("Error opening file:", err)
        return
    }
    defer file.Close()

    _, err = io.Copy(conn, file)
    if err != nil {
        fmt.Println("Error copying file:", err)
        return
    }

    response, err = client.ReadLine()
    if err != nil {
        fmt.Println("Error reading store response:", err)
        return
    }
    fmt.Println(response)

    fmt.Println("Uploaded test.txt")
}
Copy after login

Handling Errors and Exceptions

When using an FTP client for file transfer, it is very important to handle exceptions and errors. When errors occur, the client needs to be able to return error information to the user so they can identify the problems and resolve them. Below is an example using an FTP client that demonstrates how to handle errors and exceptions.

package main

import (
    "fmt"
    "io"
    "net"
    "net/textproto"
    "os"
)

func main() {
    conn, err := net.Dial("tcp", "ftp.mozilla.org:21")
    if err != nil {
        fmt.Println("Error connecting to server:", err)
        return
    }
    defer conn.Close()
    fmt.Println("Connected to server")

    client := textproto.NewConn(conn)
    _, err = client.Cmd("USER anonymous")
    if err != nil {
        fmt.Println("Error sending user command:", err)
        return
    }
    _, err = client.Cmd("PASS password")
    if err != nil {
        fmt.Println("Error sending password command:", err)
        return
    }
    fmt.Println("Logged in as anonymous")

    _, err = client.Cmd("TYPE I")
    if err != nil {
        fmt.Println("Error sending type command:", err)
        return
    }

    _, err = client.Cmd("PASV")
    if err != nil {
        fmt.Println("Error sending pasv command:", err)
        return
    }
    response, err := client.ReadLine()
    if err != nil {
        fmt.Println("Error reading pasv response:", err)
        return
    }
    fmt.Println(response)

    _, err = client.Cmd("RETR /pub/README.txt")
    if err != nil {
        fmt.Println("Error sending retr command:", err)
        return
    }

    response, err = client.ReadLine()
    if err != nil {
        fmt.Println("Error reading retr response:", err)
        return
    }
    fmt.Println(response)

    file, err := os.Create("README.txt")
    if err != nil {
        fmt.Println("Error creating file:", err)
        return
    }
    defer file.Close()

    _, err = io.Copy(file, conn)
    if err != nil {
        fmt.Println("Error copying file:", err)
        return
    }

    fmt.Println("Downloaded README.txt")

    _, err = client.Cmd("QUIT")
    if err != nil {
        fmt.Println("Error sending quit command:", err)
        return
    }
}
Copy after login

Conclusion

In this article, we have learned how to use FTP in Go language, including establishing FTP connection, using FTP client to log in to FTP server, FTP client commands, file upload and downloads, and how to handle errors and exceptions. Using FTP to transfer files may be a need of many developers. It is not difficult to use FTP client in Go language because we can use the "net" and "io" packages in the standard library to create FTP connections and perform file transfers. Next, you can use this knowledge to develop your own FTP client, or handle your own file transfer needs.

The above is the detailed content of Using FTP in Go: A Complete Guide. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template