example_test.go 1.0 KB

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