Standard library / os
os.Hostname()
Returns the OS-reported host name or an error. It is not necessarily a fully qualified DNS name. Example intended for Linux/AMD64 with Go 1.23.2 in a disposable working directory; other platforms may differ. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
func Hostname() (name string, err error)Return Value
(name string, err error)
Returns the OS-reported host name or an error.
Working Example
Example Snippetgo
package main
import ( "fmt" "os")
func must(e error) { if e != nil { panic(e) }}func main() { n, e := os.Hostname(); must(e); fmt.Println(n != "") }