forked from golang-design/clipboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
56 lines (45 loc) · 1.04 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2021 The golang.design Initiative Authors.
// All rights reserved. Use of this source code is governed
// by a MIT license that can be found in the LICENSE file.
//
// Written by Changkun Ou <changkun.de>
//go:build cgo
package clipboard_test
import (
"context"
"fmt"
"time"
"golang.design/x/clipboard"
)
func ExampleWrite() {
err := clipboard.Init()
if err != nil {
panic(err)
}
clipboard.Write(clipboard.FmtText, []byte("Hello, 世界"))
// Output:
}
func ExampleRead() {
err := clipboard.Init()
if err != nil {
panic(err)
}
fmt.Println(string(clipboard.Read(clipboard.FmtText)))
// Output:
// Hello, 世界
}
func ExampleWatch() {
err := clipboard.Init()
if err != nil {
panic(err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
changed := clipboard.Watch(context.Background(), clipboard.FmtText)
go func(ctx context.Context) {
clipboard.Write(clipboard.FmtText, []byte("你好,world"))
}(ctx)
fmt.Println(string(<-changed))
// Output:
// 你好,world
}