github.com/zxy12/go_duplicate_112_new@v0.0.0-20200807091221-747231827200/src/os/sys_windows.go (about)

     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  package os
     6  
     7  import (
     8  	"internal/syscall/windows"
     9  	"syscall"
    10  )
    11  
    12  func hostname() (name string, err error) {
    13  	// Use PhysicalDnsHostname to uniquely identify host in a cluster
    14  	const format = windows.ComputerNamePhysicalDnsHostname
    15  
    16  	n := uint32(64)
    17  	for {
    18  		b := make([]uint16, n)
    19  		err := windows.GetComputerNameEx(format, &b[0], &n)
    20  		if err == nil {
    21  			return syscall.UTF16ToString(b[:n]), nil
    22  		}
    23  		if err != syscall.ERROR_MORE_DATA {
    24  			return "", NewSyscallError("ComputerNameEx", err)
    25  		}
    26  
    27  		// If we received an ERROR_MORE_DATA, but n doesn't get larger,
    28  		// something has gone wrong and we may be in an infinite loop
    29  		if n <= uint32(len(b)) {
    30  			return "", NewSyscallError("ComputerNameEx", err)
    31  		}
    32  	}
    33  }