简体   繁体   English

如何通过单击从 Windows 中的 Go 可执行文件打开特定文件

[英]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.

相关问题 gvim:转到具有多个窗口和多个选项卡的特定文件 - gvim: go to a specific file with multiple windows and multiple tabs open 如何从Visual C ++获取特定的Windows服务可执行文件路径 - How to get the specific windows service executable file path from Visual C++ 如何通过在Windows资源管理器中直接单击文件来用我的程序打开文件 - How to open a file with my program by directly clicking on it in windows explorer 如何在 go 中的 windows 上启动外部可执行文件 - How to launch an external executable on windows in go 如何从Windows上开发的Python脚本在Mac上创建可执行文件 - How to create executable file on mac from python script developed on windows 如何测试Windows批处理文件中%PATH%中是否存在可执行文件? - How to test if an executable exists in the %PATH% from a windows batch file? 相当于Windows中的“转到文件夹窗口”,以访问位于当前目录其他位置的Windows打开提示中的文件? - equivalent of "Go to Folder window" in windows, to access a file in a windows open prompt located somewhere else from the current directory? 如何通过双击文件在空闲编辑器中打开python文件(Windows 10) - How to open a python file in idle editor by double clicking on the file (windows 10) 如何在C ++中打开文件(即.txt文件)(有点像在Windows中双击它)? - how to open a file (ie. .txt file) in C++ (kinda like double clicking it in windows)? Windows中如何检测一个文件是否为可执行文件? - How to detect whether a file is an executable file in Windows?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM