Menu Close

Go – How to get host name reported by the kernel in go

Here, We will learn how to get host name reported by the kernel in go. We can do it by using Hostname() function in os package in go golang.

Function prototype:

func Hostname() (name string, err error)

Return value:

Hostname() function in os package returns 
the host name reported by the kernel.

Example with code:

package main

import "fmt"
import "os"


func main() {

  dir, err := os.Hostname()

  fmt.Println(dir, ",", err)

}

Output:

C02YX14CLVDD , nil

To learn more about golang, Please refer given below link:

https://www.techieindoor.com/go-lang-tutorial/

References:

https://golang.org/doc/
https://golang.org/pkg/
Posted in golang, os

Leave a Reply

Your email address will not be published.

Contact Us