[英]How to open a specific file from a Go executable in Windows by clicking
I have a Go program from which I have built a Windows OS executable.我有一个 Go 程序,我从中构建了一个 Windows OS 可执行文件。 This program just takes in command line arguments and prints them.
该程序只接受命令行 arguments 并打印它们。
What I want to do is: in Windows, I want to click on a specific file (let's say a text file) and I want my Go executable to print the name of the file.我想做的是:在 Windows 中,我想点击一个特定的文件(比如说一个文本文件),我希望我的 Go 可执行文件打印文件的名称。 I am not sure how Windows passes the name of the file to an executable.
我不确定 Windows 如何将文件名传递给可执行文件。
So far, I have right-clicked on my text file > Open with > choose another program > Look for another program in my pc > selected my Go executable.到目前为止,我已经右键单击我的文本文件 > 打开方式 > 选择另一个程序 > 在我的电脑中查找另一个程序 > 选择了我的 Go 可执行文件。 But I am unsure about the next steps.
但我不确定接下来的步骤。
I want my Go executable to find the location of the text file and then manipulate it.我希望我的 Go 可执行文件找到文本文件的位置,然后对其进行操作。
Windows passes the file name to the executable in the arguments so in your case if you are opening the file named process.txt using Windows 将文件名传递给 arguments 中的可执行文件,因此在您的情况下,如果您使用打开名为 process.txt 的文件
process.txt > Open with > Choose another program > Look for another program in my pc > Selected my Go executable
I assume that your Golang code would be following following.我假设您的 Golang 代码将遵循以下内容。
package main
import (
"log"
"os"
"time"
)
func main() {
log.Println(os.Args[1:])
time.Sleep(10 * time.Second)
}
Output: Output:
[.../.../process.txt]
[.../.../process.txt]
This sample program will open up a command prompt and display the arguments on the console which will contain the file name which is passed via Open With
.该示例程序将打开一个命令提示符并在控制台上显示 arguments ,其中将包含通过
Open With
传递的文件名。 Now you will have the file path which you can process in your program.现在您将拥有可以在程序中处理的文件路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.