How to copy text to / from clipboard in Go? [closed]

醉酒当歌 提交于 2020-02-20 04:51:31

问题


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 :)


回答1:


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.



来源:https://stackoverflow.com/questions/21340920/how-to-copy-text-to-from-clipboard-in-go

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!