Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 380
Sandeep Raju Prabhakar Avatar asked Jan 24 '14 19:01

Sandeep Raju Prabhakar


People also ask

How do you paste from clipboard to text?

Pasting Text from the ClipboardPress CTRL+V to paste what you just copied to the clipboard into the edit box at the insertion point where the cursor is. Press the keystroke to read the current line, INSERT+UP ARROW, to verify that it is there.

How do you copy and paste from clipboard?

The Clipboard task pane holds many of the last images and text you copied or cut. Note: You can still do simple cut, copy, and paste the way you're used to, either by using the buttons on the ribbon or the keyboard shortcuts CTRL+X (Cut), CTRL+C (Copy), or CTRL+V (Paste).

How do I permanently copy from clipboard?

To quickly copy selected text or images to the clipboard, use hotkeys Ctrl+C or Ctrl+Insert. These hotkeys work in all Windows programs. Alternatively, you can invoke a pop-up menu by right-clicking on the selected text, and then click Copy.

How do I move data from clipboard?

To do this, select the item you wish to copy to your PC clipboard, then click "Windows Clipboard". Now, you can right-click > Paste the copied text from clipboard into a document on your PC.


1 Answers

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.

like image 193
VonC Avatar answered Oct 24 '22 02:10

VonC