golang save file to desktop

huangapple go评论90阅读模式
英文:

golang save file to desktop

问题

我正在尝试将文件保存到我的桌面,但无论我运行脚本时,它都会将文件保存在go脚本所在的目录中。

这是我正在使用的代码块:

func (d *downloader) downloadToFile(key string) {
    // 创建路径中的目录
    // 桌面路径
    desktop := "Desktop/" + d.dir
    file := filepath.Join(desktop, key)
    if err := os.MkdirAll(filepath.Dir(file), 0775); err != nil {
        panic(err)
    }

    // 设置本地文件
    fd, err := os.Create(file)
    if err != nil {
        panic(err)
    }
    defer fd.Close()

    // 使用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)
    }
}

我尝试了以下路径:

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

我似乎无法将文件保存到桌面,例如当我尝试使用以下路径时:

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

一个名为~的目录被创建,在~目录中创建了一个名为Desktop的目录,在Desktop目录中创建了d.dir,并在其中保存了所有文件。再次强调,我想要运行脚本,无论在哪里运行,我都希望d.dir文件夹及其内容被创建在桌面上。

英文:

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.

答案1

得分: 8

你可以使用这个函数来找到当前用户的个人资料 - https://godoc.org/os/user#Current

所以,根据你的操作系统,桌面将位于主目录中相应的文件夹中。

类似这样的代码:

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

另外,值得注意的是,有一个名为 github.com/mitchellh/go-homedir 的库,它是一个用于检测用户主目录的Go库,无需使用cgo,因此该库可以在交叉编译环境中使用。因此,使用它可以使你的程序更具可移植性。

英文:

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.

huangapple
  • 本文由 发表于 2015年12月19日 10:00:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/34366477.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定