简体   繁体   中英

Create a JSON or slice array in Go

I am not sure how to do this or word this. I have a variable that returns a string something like this:

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

Output:

unicode_start
unicode_stop
unlink
usleep
vi
view
ypdomainname
zcat

I'm looking for creating a JSON array or whatever is easiest to get to this final output:

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

You can do that with package encoding/json :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM