問題解決方法:
在這個程序中,我們將使用 os.ExpandEnv() 函數使用命令行上傳遞的參數擴展或格式化指定的字符串,並在控製台屏幕上打印結果。
程序/源代碼:
下麵給出了演示 os.Expand() 函數的源代碼。給定的程序在 ubuntu 18.04 操作係統上編譯和執行成功。
// Golang program to demonstrate
// the os.ExpandEnv() function
package main
import "fmt"
import "os"
func main() {
str:= "My name is $Name and live in $City."
//Format the specified string using os.ExpandEnv
strResult:= os.ExpandEnv(str)
fmt.Println(strResult)
}
輸出:
$ Name=Rahul City=Agra go run test.go My name is Rahul and live in Agra.
說明:
在上麵的程序中,我們聲明了包 main。 main 包用於告訴 Go 語言編譯器必須編譯該包並生成可執行文件。在這裏,我們導入了 "fmt" 包以使用 Printf() 函數,我們還導入了 "os" 包以使用 ExpandEnv() 函數。
在 main() 函數中,我們使用 os.ExpandEnv() 函數在運行時傳遞的參數的幫助下格式化字符串,並將結果打印在控製台屏幕上。
相關用法
- Golang os.Expand()用法及代碼示例
- Golang os.Exit()用法及代碼示例
- Golang os.Chdir()用法及代碼示例
- Golang os.Getwd()用法及代碼示例
- Golang strconv.QuoteToASCII()用法及代碼示例
- Golang io.CopyBuffer()用法及代碼示例
- Golang reflect.Kind()用法及代碼示例
- Golang time.Parse()用法及代碼示例
- Golang bits.RotateLeft16()用法及代碼示例
- Golang math.Ceil()用法及代碼示例
- Golang atomic.AddInt32()用法及代碼示例
- Golang strings.ToLowerSpecial()用法及代碼示例
- Golang time.Time.MarshalJSON()用法及代碼示例
- Golang io.Pipe()用法及代碼示例
- Golang strings.EqualFold()用法及代碼示例
- Golang reflect.FuncOf()用法及代碼示例
- Golang complx.Exp()用法及代碼示例
- Golang string.Contains用法及代碼示例
- Golang reflect.NumMethod()用法及代碼示例
- Golang time.Time.Clock()用法及代碼示例
注:本文由純淨天空篩選整理自 Golang program to demonstrate the os.ExpandEnv() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。