Skip to content

Instantly share code, notes, and snippets.

Last active Aug 13, 2021
What would you like to do?
Sample SFTP connection in Go
package main
import (
func main() {
host := ""
username := "foobar"
password := "secret"
port := ":22" // Make sure there is colon ":" prefix
config := &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
// `InsecureIgnoreHostKey` is not recommended. Consider other more secured method for verifying host key
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
// Establishing connection
conn, err := ssh.Dial("tcp", host+port, config)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to dial: %v\n", err)
// `client` is the handler for performing operations on SFTP server
client, err := sftp.NewClient(conn)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create client: %v\n", err)
// Example for checking available storage space
statVF, err := client.StatVFS("/")
if err != nil {
fmt.Fprintf(os.Stderr, "Fail to get filesystem info: %v\n", err)
fmt.Printf("Space available: %d bytes\n", statVF.FreeSpace())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment