Home »
Golang »
Golang Reference
Golang os.Hostname() Function with Examples
Golang | os.Hostname() Function: Here, we are going to learn about the Hostname() function of the os package with its usages, syntax, and examples.
Submitted by IncludeHelp, on November 23, 2021
os.Hostname()
In the Go language, the os package provides a platform-independent interface to operating system (Unix-like) functionality. The Hostname() function is an inbuilt function of the os package, it is used to get the hostname reported by the kernel.
It accepts nothing and returns a string containing the hostname reported by the kernel and error if any.
Syntax
func Hostname() (name string, err error)
Parameters
Return Value
The return type of the os.Hostname() function is (name string, err error), it returns a string containing the host name reported by the kernel and error if any.
Example
// Golang program to demonstrate the
// example of Hostname() function
package main
import (
"fmt"
"os"
)
func main() {
str, err := os.Hostname()
fmt.Printf("str: %T, %v\n", str, str)
fmt.Printf("err: %T, %v\n", err, err)
}
Output:
str: string, 8f65cc8a36d6
err: <nil>, <nil>
Golang os Package »