github.com/searKing/golang/go@v1.2.117/runtime/goroutine/example_test.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package goroutine_test 6 7 import ( 8 "fmt" 9 "sync" 10 11 "github.com/searKing/golang/go/runtime/goroutine" 12 ) 13 14 func ExampleID() { 15 fmt.Printf("%d\n", goroutine.ID()) 16 // Output: 17 // 1 18 } 19 20 func ExampleNewLock() { 21 oldDebug := goroutine.DebugGoroutines 22 goroutine.DebugGoroutines = true 23 defer func() { goroutine.DebugGoroutines = oldDebug }() 24 25 g := goroutine.NewLock() 26 g.MustCheck() 27 28 var wg sync.WaitGroup 29 wg.Add(1) 30 go func() { 31 defer wg.Done() 32 defer func() { 33 if r := recover(); r != nil { 34 fmt.Printf("panic recovered: %v\n", r) 35 } 36 }() 37 g.MustCheck() // should panic 38 }() 39 wg.Wait() 40 // Output: 41 // panic recovered: running on the wrong goroutine 42 }