I am currently developing a web app and I wanted to run shell commands off the web app itself.
Currently, my code has a very weird output and it’s definitely not showing what I wanted.
Lets say, I wanted this output to be shown on my webpage: Image: https://forum.golangbridge.org/uploads/default/original/2X/5/5a3aa500e7b18910cb39a2a720a5df286693e389.png
But currently, it’s showing this junk: Image: https://forum.golangbridge.org/uploads/default/original/2X/b/b9f0548738836c196f58a949dd656e5b472319be.png
With all that said, here’s my code:
func admin(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
I would be happy if someone could share a proper working code that includes documentation for me to learn.
Also, sorry that I couldn’t actually include the images inside here. I’m a new user
General feedback - you can include both code snippets and images here in the forums making this post both useful in the future when these external links die and making it easier for me to give feedback on mobile.
As of now it’s super hard to give feedback cause copying and pasting links in new tabs on mobile isn’t fun.
Edit - just saw your image issue was due to a new account. Is it the same for code snippets?
Images are just hard because I can’t copy paste the code to run it and see it myself. Even if you used the go playground or GitHub gists for the sample code that would help.
Anyway I believe your issue is that you are outputting an exec.Cmd object but you really want the output of that command. Try looking here - https://golang.org/pkg/os/exec/#Cmd.Output
exec.Command returns a Cmd object which you then need to call something like Output on to run it and get the output. The docs I linked have an example that should help you right under the function definition
On mobile so giving a code snippet is hard but here it goes.
uptime, err := exec.Command("uptime").Output()
if err != nil {
// Handle this
}
err = tpl_admin.ExecuteTemplate(w, "index.html", uptime)
Output returns 2 arguments (this is pretty common in go) so you need to capture those and then only pass the relevant one to the Execute template call. Make sense?
Ps - prefix all your code with 4 spaces to get a code block like mine rather than a 1-liner like your code snippets. This helps retain formatting too.