Sample code for 30+ languages & platforms
Go

Save String Variable to File on FTP Server

See more FTP Examples

Save the contents of a string variable to a file on an FTP server.

Chilkat Go Downloads

Go
    success := false

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    ftp := chilkat.NewFtp2()

    ftp.SetHostname("ftp.example.com")
    ftp.SetUsername("login")
    ftp.SetPassword("password")

    // Connect and login to the FTP server.
    success = ftp.Connect()
    if success != true {
        fmt.Println(ftp.LastErrorText())
        ftp.DisposeFtp2()
        return
    }

    // Change to the remote directory where the existing file is located.
    success = ftp.ChangeRemoteDir("junk")
    if success != true {
        fmt.Println(ftp.LastErrorText())
        ftp.DisposeFtp2()
        return
    }

    // Upload the contents of a string to a remote file.
    fileContents := "To be, or not to be: that is the question."
    remoteFilename := "hamletQuote.txt"

    success = ftp.PutFileFromTextData(remoteFilename,fileContents)
    if success != true {
        fmt.Println(ftp.LastErrorText())
        ftp.DisposeFtp2()
        return
    }

    success = ftp.Disconnect()

    fmt.Println("String Uploaded!")

    ftp.DisposeFtp2()