-1

Before I ask this, I need to apologize, I know that programmatically running this kind of thing within code is dumb, and should be moved to makefiles - but management want everything inside a binary for this job, so, cool...

I need to know how to cd to a directory, and then run:

go fmt ./...

I have found stuff like exec, etc... but the problem there is that seems to look specifically for files to work with, when I need to run a command.

Anyone done this? Got an example for me?

MickeyThreeSheds
  • 986
  • 4
  • 23
  • 42
  • 5
    `I have found stuff like exec, etc... but the problem there is that seems to look specifically for files to work with, when I need to run a command.` What? – tkausl Sep 27 '18 at 00:44
  • I guess that you wanna run you binary file like an system command ? – Truong Dang Sep 27 '18 at 00:47
  • What about this: https://nathanleclaire.com/blog/2014/12/29/shelled-out-commands-in-golang/ or https://stackoverflow.com/questions/6182369/exec-a-shell-command-in-go or https://gist.github.com/gesquive/4315ace7864c5507e3dc6ff249edc3c6 – Jerry Jeremiah Sep 27 '18 at 00:53
  • possible duplicate: https://stackoverflow.com/questions/43135919/how-to-run-a-shell-command-in-a-specific-folder-with-golang or https://stackoverflow.com/questions/34229486/how-to-execute-a-linux-built-in-command-in-golang or https://stackoverflow.com/questions/20437336/how-to-execute-system-command-in-golang-with-unknown-arguments – Jerry Jeremiah Sep 27 '18 at 00:53
  • 1
    Possible duplicate of [Exec a shell command in Go](https://stackoverflow.com/questions/6182369/exec-a-shell-command-in-go) – Scott Stensland Sep 27 '18 at 01:15
  • Read the documentation at https://godoc.org/os/exec. It includes an examples. – Charlie Tumahai Sep 27 '18 at 02:05

1 Answers1

0

When you run ‘go fmt’ you are in fact running a file just don’t know which file it is. Try running ‘which go’ on the command line. That will return the path of the file run for go. Then you can use the exec functions you found.

To change your working directory, you can follow this answer. But you might also just be able to pass a relative path.

dolan
  • 1,716
  • 11
  • 22