The Go Programming Language

Source file src/pkg/os/sys_linux.go

     1	// Copyright 2009 The Go Authors. All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	// Linux-specific
     6	
     7	package os
     8	
     9	// Hostname returns the host name reported by the kernel.
    10	func Hostname() (name string, err Error) {
    11		f, err := Open("/proc/sys/kernel/hostname")
    12		if err != nil {
    13			return "", err
    14		}
    15		defer f.Close()
    16	
    17		var buf [512]byte // Enough for a DNS name.
    18		n, err := f.Read(buf[0:])
    19		if err != nil {
    20			return "", err
    21		}
    22	
    23		if n > 0 && buf[n-1] == '\n' {
    24			n--
    25		}
    26		return string(buf[0:n]), nil
    27	}

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.