为什么Golang中的hchan结构的closed字段使用uint32类型?

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

Why Golang hchan struct closed field use uint32 type?

问题

type hchan struct {
	qcount   uint           // 队列中的总数据量
	dataqsiz uint           // 循环队列的大小
	buf      unsafe.Pointer // 指向包含dataqsiz个元素的数组
	elemsize uint16
	closed   uint32
....

我搜索了所有closed字段的引用,其值只有0和1。更令人困惑的是为什么不使用int8或其他类型?😕

为什么Golang中的hchan结构的closed字段使用uint32类型?

英文:
type hchan struct {
	qcount   uint           // total data in the queue
	dataqsiz uint           // size of the circular queue
	buf      unsafe.Pointer // points to an array of dataqsiz elements
	elemsize uint16
	closed   uint32
....

I searched the references of all closed fields, the values are only 0 and 1. What is more confusing is why not use int8 or other types?😝

为什么Golang中的hchan结构的closed字段使用uint32类型?

答案1

得分: 3

为什么Golang中的hchan结构的closed字段使用uint32类型而不是uint8类型?

请自问:

  • 使用uint8会有什么好处?(提示:没有)
  • 使用uint8会使代码更复杂吗?(提示:是的)

有时候没有“深层次”的原因。uint32运行良好并且很好。

英文:

> Why Golang hchan struct closed field use uint32 type [instead of uint8]?

Ask yourself:

  • What would be gained by using uint8? (Hint: nothing)
  • Would using uint8 make the code more complicated? (Hint: yes)

Sometimes there are no "deep" reasons. uint32 works well and is fine.

答案2

得分: 0

问问自己:

  • 你想要实现什么目标?

使用uint32也是可以的。我建议你阅读这两篇文章(1) (2),它们会简要解释数据类型。

英文:

Ask yourself:

  • What you're trying to achieve?

Using uint32 is also fine. I recommend you to read these two articles (1) (2) that will briefly explain the data types.

huangapple
  • 本文由 发表于 2021年9月15日 10:41:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/69186511.html
匿名

发表评论

匿名网友

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

确定