In Go lang, how to get the hostname of the node?
在Go lang中,如何获取节点的主机名?
In Go, you can use the os.Hostname()
function to get the hostname of the node.
在Go中 ,可以使用os.Hostname()
函数获取节点的主机名。
func Hostname() (namestring, err error)
Hostname returns the host name reported by the kernel.
主机名返回内核报告的主机名。
One example is as follows. The main.go
source code:
一个例子如下。 main.go
源代码:
package main
import (
"fmt"
"os"
)
func main() {
name, err := os.Hostname()
if err != nil {
panic(err)
}
fmt.Println("hostname:", name)
}
Run it:
运行:
$ go run main.go
hostname: host001
Answered by dtivl.
dtivl回答。
翻译自: https://www.systutorials.com/how-to-get-the-hostname-of-the-node-in-go/