- package main
- import (
- "fmt"
- "os"
- )
- func main() {
- // 获得当前工作路径 (当前工程根目录)
- dir, err := os.Getwd()
- fmt.Println(dir,err)
- // 获得任意环境变量
- pathValue := os.Getenv("Path")
- //pathValue := os.Getenv("shit")// 空字符串
- fmt.Println(pathValue)
- // 获得全部环境变量
- environ := os.Environ()
- fmt.Println(environ)
- // 打印当前设备在网络中的主机名
- name, err := os.Hostname()
- fmt.Println(name,err)
- // 获取当前用户的临时文件夹位置
- temporaryDirectory := os.TempDir()
- fmt.Println("劳资的临时文件夹是",temporaryDirectory)
- // 在 Windows 下, 顺斜线 "/" 和反斜线 "\" 都是合法的路径分隔符, 而在 Linux 下只认 "/"
- fmt.Println(os.IsPathSeparator('/'))
- fmt.Println(os.IsPathSeparator('\\'))
- fileInfo, err := os.Stat("F:/BlockChain/code/W2/day2 / 新建文本文档. txt")
- fmt.Println(fileInfo,err)
- fmt.Println(fileInfo.IsDir())//false 该文件不是文件夹
- fmt.Println(fileInfo.Name())// 新建文本文档 (2).txt
- fmt.Println(fileInfo.Size())//6 内容大小是 6 字节
- fmt.Println(fileInfo.Mode())//-rw-rw-rw- 文件主人 - 主人同组用户 - 其他人的访问权限
- fmt.Println(fileInfo.ModTime())//2018-12-25 08:53:58.54 +0800 CST 文件最后修改时间
- }
输出:
- F:\BlockChain\code <nil>
- C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\python3.5\python.exe;D:\python3.5\;D:\Git\cmd;D:\go\bin;;%GOPATH%\bin;D:\go\bin
- [=::=::\ ALLUSERSPROFILE=C:\ProgramData APPDATA=C:\Users\Administrator\AppData\Roaming asl.log=Destination=file CommonProgramFiles=C:\Program Files\Common Files CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files CommonProgramW6432=C:\Program Files\Common Files COMPUTERNAME=PC-20140101MYSM ComSpec=C:\Windows\system32\cmd.exe FP_NO_HOST_CHECK=NO GOPATH=F:\BlockChain\code\W1\day4\ourmath;D:\go GOROOT=D:\Go HOMEDRIVE=C: HOMEPATH=\Users\Administrator LOCALAPPDATA=C:\Users\Administrator\AppData\Local LOGONSERVER=\\PC-20140101MYSM NUMBER_OF_PROCESSORS=8 OS=Windows_NT Path=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\python3.5\python.exe;D:\python3.5\;D:\Git\cmd;D:\go\bin;;%GOPATH%\bin;D:\go\bin PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC PROCESSOR_ARCHITECTURE=AMD64 PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 94 Stepping 3, GenuineIntel PROCESSOR_LEVEL=6 PROCESSOR_REVISION=5e03 ProgramData=C:\ProgramData ProgramFiles=C:\Program Files ProgramFiles(x86)=C:\Program Files (x86) ProgramW6432=C:\Program Files PSModulePath=C:\Windows\system32\WindowsPowerShell\v1.0\Modules\ PUBLIC=C:\Users\Public SESSIONNAME=Console SystemDrive=C: SystemRoot=C:\Windows TEMP=C:\Users\ADMINI~1\AppData\Local\Temp TMP=C:\Users\ADMINI~1\AppData\Local\Temp USERDOMAIN=PC-20140101MYSM USERNAME=Administrator USERPROFILE=C:\Users\Administrator windir=C:\Windows windows_tracing_flags=3 windows_tracing_logfile=C:\BVTBin\Tests\installpackage\csilogfile.log _DFX_INSTALL_UNSIGNED_DRIVER=1]
- PC-20140101MYSM <nil>
劳资的临时文件夹是 C:\Users\ADMINI~1\AppData\Local\Temp
true
true
&{新建文本文档. txt 32 {108929552 30775863} {108929552 30775863} {108929552 30775863} 0 0 0 0 {0 0} F:/BlockChain/code/W2/day2 / 新建文本文档. txt 0 0 0 false} <nil>
false
新建文本文档. txt
- 0
- -rw-rw-rw-
- 2019-11-13 23:28:40.0106 +0800 CST
来源: http://www.bubuko.com/infodetail-3286781.html