2

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?

cmaher
  • 5,100
  • 1
  • 22
  • 34
Matt
  • 22,721
  • 17
  • 71
  • 112
  • Do you mean the Windows clipboard? – theglauber Jun 18 '13 at 21:32
  • @theglauber Cross-platform. – Matt Jun 18 '13 at 22:50
  • 1
    I don't think that exists. It would have to be provided by the underlying runtime, and there is no common runtime between Go and other no-Go applications. The example you saw, uses Windows' [user32.dll](http://en.wikipedia.org/wiki/Microsoft_Windows_library_files#USER32.DLL) – theglauber Jun 19 '13 at 18:42
  • 2
    It doesn't mean it can't be done, but it would require platform-aware code. – theglauber Jun 19 '13 at 18:52

2 Answers2

3

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.

Dave C
  • 7,729
  • 4
  • 49
  • 65
mattn
  • 7,571
  • 30
  • 54
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.

Mike Reedell
  • 1,567
  • 15
  • 23