如何在Go中将文本复制到剪贴板或从剪贴板复制文本?

如何在Go中将文本复制到剪贴板或从剪贴板复制文本?

问题描述:

在我的Go语言命令行应用程序中,我需要能够使用Go将某些文本片段复制到系统剪贴板。基本上像 PyperClip ,但对于Go。

In my Go language command line application, I need the ability to copy certain snippets of text to the system clipboard using Go. Basically something like PyperClip, but for Go.

我正在寻找与平台无关的解决方案!任何帮助都会很棒:)

I'm looking for a platform agnostic solution! Any help would be great :)

一个项目(仅适用于Windows和Mac)似乎正在满足您的要求: atotto / clipboard

One project (just for Windows and Mac) seems approaching what you want: atotto/clipboard.


为Go复制和粘贴到剪贴板。

Provide copying and pasting to the Clipboard for Go.



func ReadAll() (string, error)
func WriteAll(text string) error

Linux支持在以下 clipboard_linux.go 类中: xsel --output / input --clipboard 系统命令的简单包装。

Linux support is in this clipboard_linux.go class: a simple wrapper to xsel --output/input --clipboard system command.

另一种方法:尝试利用第三方库,例如 GLFW

Another approach: try and take advantage of third-party libraries, like GLFW:


免费,开源,用于打开窗口,创建OpenGL上下文和管理输入的多平台库

a free, Open Source, multi-platform library for opening a window, creating an OpenGL context and managing input

它的Go包装器 glfw3 确实提供了 clipboard.go 文件,并使用了多平台方法。

Its Go wrapper glfw3 does provide a clipboard.go file, with supposedly multi-platform methods.

func (w *Window) SetClipboardString(str string)
func (w *Window) GetClipboardString() (string, error)

但这是在GLFW窗口的上下文中,当然不是任何shell窗口。

But that would be in the context of GLFW windows, not any shell window of course.