簡體   English   中英

在Go中創建JSON或slice數組

[英]Create a JSON or slice array in Go

我不知道該怎么做或說這個。 我有一個變量返回類似這樣的字符串:

unixCommands := exec.Command("ls", "/bin")
unixCommandsout, err := unixCommands.Output()
unixCommandsstring := string(unixCommandsout)
fmt.Printf(unixCommandsstring)

輸出:

unicode_start
unicode_stop
unlink
usleep
vi
view
ypdomainname
zcat

我正在尋找創建JSON數組或最簡單的方法來獲得此最終輸出:

["unicode_start", "unicode_stop", "unlink", "usleep", "vi", "view", "ypdomainname", "zcat"]

您可以使用包encoding/json做到這一點:

outputSlice := strings.Split(unixCommandsstring,"\n")
js,_ := json.Marshal(outputSlice)
fmt.Print(string(js))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM