go语言获取系统盘符的方法
本文实例讲述了go语言获取系统盘符的方法。分享给大家供大家参考。具体如下:
涉及golang调用winapi获取系统盘符的技巧。代码如下:
//csprojectmain.go packagemain import( "fmt" ."strconv" "syscall" ) funcGetLogicalDrives()[]string{ kernel32:=syscall.MustLoadDLL("kernel32.dll") GetLogicalDrives:=kernel32.MustFindProc("GetLogicalDrives") n,_,_:=GetLogicalDrives.Call() s:=FormatInt(int64(n),2) vardrives_all=[]string{"A:","B:","C:","D:","E:","F:","G:","H:","I:","J:","K:","L:","M:","N:","O:","P:","Q:","R:","S:","T:","U:","V:","W:","X:","Y:","Z:"} temp:=drives_all[0:len(s)] vard[]string fori,v:=ranges{
ifv==49{ l:=len(s)-i-1 d=append(d,temp[l]) } } vardrives[]string fori,v:=ranged{ drives=append(drives[i:],append([]string{v},drives[:i]...)...) } returndrives } funcmain(){ fmt.Println(GetLogicalDrives()) }