Hi,
I am pretty new to golang, so not sure how to structure my code.
What I have is a program that connects to different servers and does some stuff there, like listing a file, creating a directory, uploading a file, executing a command… But those steps are not sequential, so what I am doing now is configuring and establishing the connection every time I need this.
So what I am wondering is whether would be better to have a function that sets everything up and then I just call this function when I need to do something (since I am doing stuff in to different servers, I guess I would need two functions.)
Methods not shared by sftp.Client and ssh.Session can be called on connection, e.g., conn.Mkdir(path). Shared methods need to be called with, e.g., conn.Session.Close(); though you should consider writing wrappers for those. conn.Close() would ideally close all the related resources.
Then you can name the connections after the servers and get code like backupServer.Mkdir(path).
If you need to deal with reconnecting, you can add that code into wrapper functions on connection.