2

I'm trying to saving files to my desktop, however whenever I run my script it saves the file in whatever directory the go script is located in.

This is the chunk of code i'm working with

func (d *downloader) downloadToFile(key string) {
    // Create the directories in the path
    // desktop path
    desktop := "Desktop/" + d.dir
    file := filepath.Join(desktop, key)
    if err := os.MkdirAll(filepath.Dir(file), 0775); err != nil {
        panic(err)
    }

    // Setup the local file
    fd, err := os.Create(file)
    if err != nil {
        panic(err)
    }
    defer fd.Close()

    // Download the file using the AWS SDK
    fmt.Printf("Downloading s3://%s/%s to %s...\n", d.bucket, key, file)
    params := &s3.GetObjectInput{Bucket: &d.bucket, Key: &key}
    d.Download(fd, params)
    _, e := d.Download(fd, params)
    if e != nil {
        panic(e)
    }
}

I've tried the path

desktop := "Desktop/" + d.dir
desktop := "/Desktop/" + d.dir
desktop := "Desktop/" + d.dir
desktop := "~/Desktop/ + d.dir

I can't seem to get the files to save to the desktop, for instance when i tried

desktop := "~/Desktop/ + d.dir

A directory ~ was created, inside of ~ Desktop was created, inside of Desktop the d.dir was created, and in there all the files. Again I want to run the script an no matter where I run it I want to d.dir folder with it's contents so be created on the desktop.

0

2 Answers 2

7

You can find current user profile using this function - https://godoc.org/os/user#Current

So, depending on your OS, desktop will be in corresponding folder in home directory.

something like this

   myself, error := user.Current()
   if error != nil {
     panic(error)
   }
   homedir := myself.HomeDir
   desktop := homedir+"/Desktop/" + d.dir

also it is worth notice, that there is a github.com/mitchellh/go-homedir library that is a Go library for detecting the user's home directory without the use of cgo, so the library can be used in cross-compilation environments. So using it can be better to make your program more portable.

Sign up to request clarification or add additional context in comments.

3 Comments

Awesome thank you. I'm still learning the core library. I've been working with python.
@msanti, not that this will break in some future Windows version when it will decide to rename "Desktop" to something else. The real way to go it to use the "special folders" facility of the Windows shell integration API to get the location of the desktop folder on the Windows your program is running on. Unfortunately this requires certain skills in C, Windows API and calling it from Go (the latter is easy). I'd ask a question on the go-nuts mailing list.
Oh it's fine. I'm a Linux, Unix user and that's where this script will remain lol
0

on winodows, HomeDir/Desktop is unreliable, should:

windows.KnownFolderPath(windows.FOLDERID_Desktop, 0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.