问题描述

syscall
syscall

推荐答案

syscall 包的官方在线 godoc 位于 https://golang.org/pkg/syscall/ 似乎记录了 Linux API,因此在线查找资源有些困难.

The official online godoc for the syscall package at https://golang.org/pkg/syscall/ seems to document Linux APIs so it is somewhat hard to find the resources online.

GOOSGOARCH
GOOSGOARCH

例如,在 Linux shell 中运行以下命令允许 godoc 相信它在 Windows 上运行,因此记录相应的文件:

For example, the following commands run in a Linux shell allow godoc to believe it runs on Windows, and therefore document the corresponding files:

export GOOS=windows
export GOARCH=amd64
godoc -http=:8080

在浏览器中访问 http://localhost:8080/pkg/syscall/ 会显示 Windows系统调用 API 文档.

Accessing http://localhost:8080/pkg/syscall/ in a browser shows the Windows syscall API docs.

GetPhysicallyInstalledSystemMemory
GetPhysicallyInstalledSystemMemory

显然,Windows Go syscall 包中不存在此函数,因此无法直接调用它.

Apparently, this function does not exist in the Windows Go syscall package, so calling it directly is not possible.

kernel32.dll
kernel32.dll
//+build windows
package main

import (
    "fmt"
    "syscall"
    "unsafe"
)

func main() {
    var mod = syscall.NewLazyDLL("kernel32.dll")
    var proc = mod.NewProc("GetPhysicallyInstalledSystemMemory")
    var mem uint64

    ret, _, err := proc.Call(uintptr(unsafe.Pointer(&mem)))
    fmt.Printf("Ret: %d, err: %v, Physical memory: %d\n", ret, err, mem)
}

运行时,输出:

返回:1,错误:L'opération a réussi.,物理内存:16777216

Ret: 1, err: L’opération a réussi., Physical memory: 16777216

该值以千字节为单位,因此除以 1048576 (1024*1024) 即可获得以千兆字节为单位的值.

The value is given in kilobytes, so divide by 1048576 (1024*1024) to obtain a value in gigabytes.

这篇关于使用 Golang 查询 Windows 中的总物理内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!