示例#1
0
func ExampleGo() {
	var (
		mgr            = gls.NewContextManager()
		request_id_key = gls.GenSym()
	)

	MyLog := func() {
		if request_id, ok := mgr.GetValue(request_id_key); ok {
			fmt.Println("My request id is:", request_id)
		} else {
			fmt.Println("No request id found")
		}
	}

	mgr.SetValues(gls.Values{request_id_key: "12345"}, func() {
		var wg sync.WaitGroup
		wg.Add(1)
		go func() {
			defer wg.Done()
			MyLog()
		}()
		wg.Wait()
		wg.Add(1)
		gls.Go(func() {
			defer wg.Done()
			MyLog()
		})
		wg.Wait()
	})

	// Output: No request id found
	// My request id is: 12345
}
示例#2
0
func ExampleContextManager_SetValues() {
	var (
		mgr            = gls.NewContextManager()
		request_id_key = gls.GenSym()
	)

	MyLog := func() {
		if request_id, ok := mgr.GetValue(request_id_key); ok {
			fmt.Println("My request id is:", request_id)
		} else {
			fmt.Println("No request id found")
		}
	}

	mgr.SetValues(gls.Values{request_id_key: "12345"}, func() {
		MyLog()
	})
	MyLog()

	// Output: My request id is: 12345
	// No request id found
}