Go cpu.Counts(true)

8 min read

cpu.Counts(true) 是一个 Go 语言的函数,它返回系统中的物理或逻辑核心的数量。它是 shirou/gopsutil 包的一部分,用于获取系统的 CPU 信息。如果传入 true,它会返回逻辑核心的数量,如果传入 false,它会返回物理核心的数量1=。例如:

import (
  "fmt"
  "github.com/shirou/gopsutil/cpu"
)

func main() {
  logicalCores, _ := cpu.Counts(true)
  physicalCores, _ := cpu.Counts(false)
  fmt.Println("Logical cores:", logicalCores)
  fmt.Println("Physical cores:", physicalCores)
}

逻辑核心的数量是物理核心的数量乘以超线程数,超线程是一种技术,可以让一个物理核心同时处理两个线程。