Go FAQ中说“没有goroutine ID”,但我们可以从runtime.Stack中获取它。它是什么?

huangapple go评论82阅读模式
英文:

Go FAQ says "there is no goroutine ID", but we can get it from runtime.Stack. What is it?

问题

Go FAQ回答了一个问题:“为什么没有goroutine ID?”

Goroutines没有名称,它们只是匿名的工作线程。它们对程序员没有提供任何唯一标识符、名称或数据结构。

我对“它们没有提供任何唯一标识符”的解释并不满意,因为我们似乎可以通过使用runtime.Stack()来获取goroutine ID。

问题:

  1. 在Go FAQ的回答中,“unique identifier”和通过runtime.Stack提取的goroutine ID有什么区别?

  2. 为什么Go FAQ回答“它们没有提供任何唯一标识符”?

我想清楚地理解“为什么没有goroutine ID?”的答案!

runtime.Stack()似乎提供了goroutine ID。这里有一个示例代码。

此外,还有一种使用eBPF获取goroutine ID的方法。

https://stackoverflow.com/questions/74383816/how-to-get-goroutine-id-with-ebpf

英文:

Go FAQ answers the question, "Why is there no goroutine ID?"

> Goroutines do not have names; they are just anonymous workers. They expose no unique identifier, name, or data structure to the programmer.
>
> https://go.dev/doc/faq#no_goroutine_id

I am not convinced by the explanation "They expose no unique identifier" because it appears that we can get goroutine id by using runtime.Stack().

Question

  1. What's the difference between "unique identifier" in Go FAQ answer and goroutine id extracted by runtime.Stack?

  2. Why does Go FAQ answer "They expose no unique identifier"?

I want to clearly understand "Why is there no goroutine ID?" answer!

runtime.Stack() seems to provide goroutine id. <https://go.dev/play/p/5b6FD7C8S6->

package main

import (
	&quot;bytes&quot;
	&quot;errors&quot;
	&quot;fmt&quot;
	&quot;runtime&quot;
	&quot;strconv&quot;
)

func main() {
	fmt.Println(goid())

	done := make(chan struct{})
	go func() {
		fmt.Println(goid())
		done &lt;- struct{}{}
	}()
	go func() {
		fmt.Println(goid())
		done &lt;- struct{}{}
	}()
	&lt;-done
	&lt;-done
	close(done)
}

var (
	goroutinePrefix = []byte(&quot;goroutine &quot;)
	errBadStack     = errors.New(&quot;invalid runtime.Stack output&quot;)
)

func goid() (int, error) {
	buf := make([]byte, 32)
	n := runtime.Stack(buf, false)
	buf = buf[:n]
	// goroutine 1 [running]: ...

	buf, ok := bytes.CutPrefix(buf, goroutinePrefix)
	if !ok {
		return 0, errBadStack
	}

	i := bytes.IndexByte(buf, &#39; &#39;)
	if i &lt; 0 {
		return 0, errBadStack
	}

	return strconv.Atoi(string(buf[:i]))
}

And, there is another approach getting goroutine id by eBPF.

https://stackoverflow.com/questions/74383816/how-to-get-goroutine-id-with-ebpf

答案1

得分: 3

“唯一标识符”在Go FAQ答案中的含义与通过runtime.Stack提取的goroutine id有什么区别?

FAQ中指出,goroutine不会向程序员公开唯一标识符、名称或数据结构。

运行时确实需要一种方式来标识goroutine,但没有一种受支持的方式来获取该标识符。

Go文档没有指定由runtime.Stack返回的堆栈跟踪的格式或goroutine编号的含义。问题中的代码可能现在可以检索到唯一的id,但不能保证将来代码仍然能够这样做。

英文:

> What's the difference between "unique identifier" in Go FAQ answer and goroutine id extracted by runtime.Stack?

The FAQ states that goroutines do not expose a unique identifier, name, or data structure to the programmer.

The runtime does need a way to identify a goroutine, but there is not a supported way to get that identifier.

The Go documentation does not specify the format of the stacktrace returned by runtime.Stack or the meaning of the goroutine number. The code in the question may retrieve a unique id now, but there's no guarantee that the code will do so in the future.

答案2

得分: 2

Goroutines没有名称,它们只是匿名的工作线程。它们不向程序员公开任何唯一标识符、名称或数据结构。

让我们用更清晰的方式重新解释这句话来回答你的问题(用我自己的话):

Go语言没有提供任何用于唯一标识Goroutines的接口。

语言和标准库提供的接口受到Go项目的严格政策的约束,主要包括:

  • 接口按照其所说的那样工作
  • 接口将与未来的Go 1.x版本保持兼容

因此,我们应该假设任何识别goroutines的方法都不受这些承诺的影响。换句话说,Go项目不负责确保它实际上在现在或将来的任何时候都能正常工作。

英文:

> Goroutines do not have names; they are just anonymous workers. They expose no unique identifier, name, or data structure to the programmer.

Let's reinterpret this quote in a way that more clearly answer your question (in my own words):

> The Go language does not provide any interface for uniquely identifying Goroutines.

Interfaces provided by the language and standard library are subject to strong policies from the Go project, mainly:

  • The interface does what it says it does
  • The interface will remain compatible with future Go 1.x versions

Therefore, we should assume that any method of identifying goroutines does not benefit from those promises. In other words, the Go project takes no responsibility for ensuring that it actually works now, or at any point in the future.

huangapple
  • 本文由 发表于 2023年5月11日 07:30:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223208.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定