goroutine_id

package module
v0.0.0-...-98b48a6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 31, 2023 License: MIT Imports: 4 Imported by: 1

README

go-goroutine-id

一、这是啥?解决了啥问题?

在Go中是不推荐使用Goroutine ID的,但是仍然有一些场景有Goroutine ID会更好,这个库就提供了API能够获取协程的ID。

二、安装

go get -u github.com/golang-infrastructure/go-goroutine-id

三、API示例

package main

import (
	"fmt"
	goroutine_id "github.com/golang-infrastructure/go-goroutine-id"
	"sync"
)

func main() {

	var goroutineId int
	var err error

	// Start a new coroutine and see what its id is
	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		defer wg.Done()
		goroutineId, err = goroutine_id.GetGoroutineID()
		if err != nil {
			panic(err)
		}
	}()
	wg.Wait()
	fmt.Println(fmt.Sprintf("Goroutine ID: %d", goroutineId))
	// Output:
	// Goroutine ID: 6

	// Look at the id of the main coroutine
	goroutineId, err = goroutine_id.GetGoroutineID()
	if err != nil {
		panic(err)
	}
	fmt.Println(fmt.Sprintf("Main Goroutine ID: %d", goroutineId))
	// Output:
	// Main Goroutine ID: 1

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetGoroutineID

func GetGoroutineID() (int, error)

GetGoroutineID 获取当前协程的ID

func GetGoroutineIDAsString

func GetGoroutineIDAsString() string

GetGoroutineIDAsString 获取当前协程的ID,以字符串的形式返回

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL