File tree Expand file tree Collapse file tree 4 files changed +12
-15
lines changed
Expand file tree Collapse file tree 4 files changed +12
-15
lines changed Original file line number Diff line number Diff line change 6868 debug = false
6969 errUnavailable = errors .New ("clipboard unavailable" )
7070 errUnsupported = errors .New ("unsupported format" )
71+ errNoCgo = errors .New ("clipboard: cannot use when CGO_ENABLED=0" )
7172)
7273
7374// Format represents the format of clipboard data.
8586 // Due to the limitation on operating systems (such as darwin),
8687 // concurrent read can even cause panic, use a global lock to
8788 // guarantee one read at a time.
88- lock = sync.Mutex {}
89- initOnce sync.Once
89+ lock = sync.Mutex {}
90+ initOnce sync.Once
9091 initError error
9192)
9293
@@ -95,10 +96,10 @@ var (
9596// target system lacks required dependency, such as libx11-dev in X11
9697// environment. For example,
9798//
98- // err := clipboard.Init()
99- // if err != nil {
100- // panic(err)
101- // }
99+ // err := clipboard.Init()
100+ // if err != nil {
101+ // panic(err)
102+ // }
102103//
103104// If Init returns an error, any subsequent Read/Write/Watch call
104105// may result in an unrecoverable panic.
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ package clipboard
55import "context"
66
77func initialize () error {
8- panic ( "clipboard: cannot use when CGO_ENABLED=0" )
8+ return errNoCgo
99}
1010
1111func read (t Format ) (buf []byte , err error ) {
Original file line number Diff line number Diff line change @@ -34,14 +34,9 @@ func TestClipboardInit(t *testing.T) {
3434 t .Skip ("Windows does not need to check for cgo" )
3535 }
3636
37- defer func () {
38- if r := recover (); r != nil {
39- return
40- }
41- t .Fatalf ("expect to fail when CGO_ENABLED=0" )
42- }()
43-
44- clipboard .Init ()
37+ if err := clipboard .Init (); ! errors .Is (err , clipboard .ErrCgoDisabled ) {
38+ t .Fatalf ("expect ErrCgoDisabled, got: %v" , err )
39+ }
4540 })
4641 t .Run ("with-cgo" , func (t * testing.T ) {
4742 if val , ok := os .LookupEnv ("CGO_ENABLED" ); ok && val == "0" {
Original file line number Diff line number Diff line change @@ -10,4 +10,5 @@ package clipboard
1010var (
1111 Debug = debug
1212 ErrUnavailable = errUnavailable
13+ ErrCgoDisabled = errNoCgo
1314)
You can’t perform that action at this time.
0 commit comments