Go语言自学系列 | golang标准库os包和环境相关的方法

Posted COCOgsta

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Go语言自学系列 | golang标准库os包和环境相关的方法相关的知识,希望对你有一定的参考价值。

视频来源:B站《golang入门到项目实战 [2021最新Go语言教程,没有废话,纯干货!持续更新中...]》

一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持!

附上汇总贴:Go语言自学系列 | 汇总_COCOgsta的博客-CSDN博客_go语言自学


package main

import (
    "fmt"
    "os"
)

func main() 
    // 获得所有环境变量
    s := os.Environ()
    fmt.Printf("s: %v\\n", s)
    // 获得某个环境变量
    s2 := os.Getenv("GOPATH")
    fmt.Printf("s2: %v\\n", s2)
    // 设置环境变量
    os.Setenv("env1", "env1")
    s2 = os.Getenv("aaa")
    fmt.Printf("s2: %v\\n", s2)
    fmt.Println("--------------")

    // 查找
    s3, b := os.LookupEnv("env")
    fmt.Printf("b: %v\\n", b)
    fmt.Printf("s3: %v\\n", s3)

    // 替换
    os.Setenv("NAME", "gopher")
    os.Setenv("BURROW", "/usr/gopher")

    os.ExpandEnv("$NAME lives in $BURROW.")

    // 清空环境变量
    // os.Clearenv()

运行结果

[Running] go run "/Users/guoliang/SynologyDrive/软件开发/go/golang入门到项目实战/goproject/360duote.com/pro01/test.go"
s: [GOMODCACHE=/Users/guoliang/go/pkg/mod SHELL=/bin/bash TMPDIR=/var/folders/h4/4_rftxmj2nn875h0rm72m9r80000gn/T/ Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.pmq8dkywsP/Render ORIGINAL_XDG_CURRENT_DESKTOP=undefined LC_ALL=en_US.UTF-8 USER=guoliang VSCODE_VERBOSE_LOGGING=true SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.7hhDv7wtSU/Listeners VSCODE_PIPE_LOGGING=true HOMEBREW_NO_AUTO_UPDATE=true __CF_USER_TEXT_ENCODING=0x1F5:0x19:0x34 VSCODE_LOG_STACK=false VSCODE_AMD_ENTRYPOINT=vs/workbench/api/node/extensionHostProcess ELECTRON_RUN_AS_NODE=1 PATH=/Library/Frameworks/Python.framework/Versions/3.6/bin:/Users/guoliang/anaconda/bin:/Users/guoliang/anaconda/bin:/Users/guoliang/anaconda/bin:/Users/guoliang/anaconda/bin:/anaconda3/bin:/usr/local/phantomjs-2.1.1-macosx/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Applications/Wireshark.app/Contents/MacOS:/usr/local/apache-maven-3.2.2/bin:/Users/guoliang/java/mongodb-osx-x86_64-3.4.19/bin:/Library/tomcat/bin:/usr/local/mysql/bin:/Library/scala-2.12.5//bin:/usr/local/go/bin GOPROXY=https://goproxy.cn _=/usr/local/go/bin/go PWD=/Users/guoliang/SynologyDrive/软件开发/go/golang入门到项目实战/goproject/360duote.com/pro01 VSCODE_HANDLES_UNCAUGHT_ERRORS=true JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home LANG=en_US.UTF-8 XPC_FLAGS=0x0 VSCODE_LOG_NATIVE=false XPC_SERVICE_NAME=com.microsoft.VSCode.26104 SHLVL=2 M2_HOME=/usr/local/apache-maven-3.2.2 HOME=/Users/guoliang APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL=1 VSCODE_IPC_HOOK_EXTHOST=/var/folders/h4/4_rftxmj2nn875h0rm72m9r80000gn/T/vscode-ipc-2d2a482e-8b16-4704-bcf5-33918d336d46.sock VSCODE_NLS_CONFIG="locale":"zh-cn","availableLanguages":"*":"zh-cn","_languagePackId":"5b383b9d184f7cbe3cfd7572cd7df52b.zh-cn","_translationsConfigFile":"/Users/guoliang/Library/Application Support/Code/clp/5b383b9d184f7cbe3cfd7572cd7df52b.zh-cn/tcf.json","_cacheRoot":"/Users/guoliang/Library/Application Support/Code/clp/5b383b9d184f7cbe3cfd7572cd7df52b.zh-cn","_resolvedLanguagePackCoreLocation":"/Users/guoliang/Library/Application Support/Code/clp/5b383b9d184f7cbe3cfd7572cd7df52b.zh-cn/b5205cc8eb4fbaa726835538cd82372cc0222d43","_corruptedFile":"/Users/guoliang/Library/Application Support/Code/clp/5b383b9d184f7cbe3cfd7572cd7df52b.zh-cn/corrupted.info","_languagePackSupport":true LOGNAME=guoliang CLASSPATH=.:/Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home/lib/tools.jar VSCODE_IPC_HOOK=/Users/guoliang/Library/Application Support/Code/1.65.0-main.sock VSCODE_CODE_CACHE_PATH=/Users/guoliang/Library/Application Support/Code/CachedData/b5205cc8eb4fbaa726835538cd82372cc0222d43 GOPATH=/Users/guoliang/go VSCODE_PID=804 VSCODE_CWD=/]
s2: /Users/guoliang/go
s2: 
--------------
b: false
s3: 

以上是关于Go语言自学系列 | golang标准库os包和环境相关的方法的主要内容,如果未能解决你的问题,请参考以下文章

Go语言自学系列 | golang标准库os模块 - File文件读操作

Go语言自学系列 | golang标准库os模块 - File文件写操作

Go语言自学系列 | golang标准库bytes

Go语言自学系列 | golang标准库builtin

Go语言自学系列 | golang标准库errors

Go语言自学系列 | golang标准库bufio