How to use the clipboard in Go?

2019-07-28 00:56发布

问题:

How can I copy to the clipboard, or get data from it, using Go?

I'm a bit concerned that there are no results in the documentation and Google has yielded me this result, but I want this to work cross-platform... then I found this playground snippet (from that page) but it doesn't compile (because the "unsafe" package can't be used in the playground, I get this, but it still looks platform-dependent).

So is this even possible, cross-platform?

回答1:

You'll have to os.exec(..) out to the platform-specific command for copying-to/pasting-from the clipboard.

Or you could use platform-specific libraries to do this, but I'm pretty sure there's no platform agnostic way to access the clipboard.



回答2:

Clipboard is a platform-specific thing, so you should use a third party package. For example, use go-gtk.

https://github.com/mattn/go-gtk/tree/master/_example/clipboard

This will work on platforms which are supported by GTK.



标签: go clipboard