GO语言常用标准库02---os包

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:BlockChaincode <nil>
C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;D:python3.5python.exe;D:python3.5;D:Gitcmd;D:goin;;%GOPATH%in;D:goin
[=::=:: ALLUSERSPROFILE=C:ProgramData APPDATA=C:UsersAdministratorAppDataRoaming asl.log=Destination=file CommonProgramFiles=C:Program FilesCommon Files CommonProgramFiles(x86)=C:Program Files (x86)Common Files CommonProgramW6432=C:Program FilesCommon Files COMPUTERNAME=PC-20140101MYSM ComSpec=C:Windowssystem32cmd.exe FP_NO_HOST_CHECK=NO GOPATH=F:BlockChaincodeW1day4ourmath;D:go GOROOT=D:Go HOMEDRIVE=C: HOMEPATH=UsersAdministrator LOCALAPPDATA=C:UsersAdministratorAppDataLocal LOGONSERVER=\PC-20140101MYSM NUMBER_OF_PROCESSORS=8 OS=Windows_NT Path=C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;D:python3.5python.exe;D:python3.5;D:Gitcmd;D:goin;;%GOPATH%in;D:goin 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:Windowssystem32WindowsPowerShellv1.0Modules PUBLIC=C:UsersPublic SESSIONNAME=Console SystemDrive=C: SystemRoot=C:Windows TEMP=C:UsersADMINI~1AppDataLocalTemp TMP=C:UsersADMINI~1AppDataLocalTemp USERDOMAIN=PC-20140101MYSM USERNAME=Administrator USERPROFILE=C:UsersAdministrator windir=C:Windows windows_tracing_flags=3 windows_tracing_logfile=C:BVTBinTestsinstallpackagecsilogfile.log _DFX_INSTALL_UNSIGNED_DRIVER=1]
PC-20140101MYSM <nil>
劳资的临时文件夹是 C:UsersADMINI~1AppDataLocalTemp
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

  

原文地址:https://www.cnblogs.com/yunweiqiang/p/11854115.html