user_windows.go (799B)
1 //go:build windows && !appengine 2 3 package pqutil 4 5 import ( 6 "path/filepath" 7 "syscall" 8 ) 9 10 func User() (string, error) { 11 // Perform Windows user name lookup identically to libpq. 12 // 13 // The PostgreSQL code makes use of the legacy Win32 function GetUserName, 14 // and that function has not been imported into stock Go. GetUserNameEx is 15 // available though, the difference being that a wider range of names are 16 // available. To get the output to be the same as GetUserName, only the 17 // base (or last) component of the result is returned. 18 var ( 19 name = make([]uint16, 128) 20 pwnameSz = uint32(len(name)) - 1 21 ) 22 err := syscall.GetUserNameEx(syscall.NameSamCompatible, &name[0], &pwnameSz) 23 if err != nil { 24 return "", err 25 } 26 s := syscall.UTF16ToString(name) 27 return filepath.Base(s), nil 28 }