Overview
‘os’ package of golang provides a Hostname function that can be used to get the hostname that is reported by the kernel
Below is the signature of this method. It returns an error if not able to successfully get the hostname
func Hostname() (name string, err error)
Let’s see a working code
Code
package main
import (
"fmt"
"os"
)
func main() {
hostname, err := os.Hostname()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Hostname: %s", hostname)
}
Output:
Hostname: