gitee.com/quant1x/gox@v1.21.2/threadlocal/gid_test.go (about)

     1  package threadlocal
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  )
     7  
     8  func ExampleGo() {
     9  	Init()
    10  	var wg sync.WaitGroup
    11  	wg.Add(2)
    12  
    13  	Set(`key`, `main value`)
    14  	Go(func() {
    15  		Set(`key`, `value 1`)
    16  		Go(func() {
    17  			Set(`key`, `value 2`)
    18  			v, _ := Get(`key`)
    19  			fmt.Printf("g2 = %s\n", v)
    20  			Delete(`key`)
    21  			wg.Done()
    22  		})
    23  		v, _ := Get(`key`)
    24  		fmt.Printf("g1 = %s\n", v)
    25  		Delete(`key`)
    26  		wg.Done()
    27  	})
    28  	wg.Wait()
    29  	v, _ := Get(`key`)
    30  	fmt.Printf("main = %v\n", v)
    31  	// Unordered output:
    32  	// g1 = value 1
    33  	// g2 = value 2
    34  	// main = main value
    35  }