How to copy text to / from clipboard in Go?

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

How to copy text to / from clipboard in Go?

问题

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

我正在寻找一个跨平台的解决方案!任何帮助都将不胜感激 How to copy text to / from clipboard in 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 How to copy text to / from clipboard in Go?

答案1

得分: 12

有一个项目(仅适用于Windows和Mac)似乎接近你想要的:atotto/clipboard

提供了用于Go语言的复制和粘贴到剪贴板的功能。

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

Linux支持在这个clipboard_linux.go类中:它是一个简单的包装器,用于调用xsel --output/input --clipboard系统命令。


另一种方法是尝试利用第三方库,比如GLFW

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

它的Go语言封装glfw3提供了一个clipboard.go文件,其中包含了跨平台的方法。

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

但这只适用于GLFW窗口的上下文,而不是任何shell窗口。

英文:

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

> Provide copying and pasting to the Clipboard for Go.

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

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


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

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

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)

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

huangapple
  • 本文由 发表于 2014年1月25日 03:30:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/21340920.html
匿名

发表评论

匿名网友

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

确定