-1

Few days back code was showing like proper Go syntax. Not sure which command I ran but now main.go code is showing like this. May be this go build -gcflags='all=-N -l' -o main.go command converted code to binaries compile code. enter image description here

Any idea how to recover back actual code ?

Avinash Dalvi
  • 8,551
  • 7
  • 27
  • 53
  • 2
    You overwrote your source file with binary (-o main.go tells compiler to save output as main.go). Unless you have backup your source code is gone. – blami Aug 14 '23 at 03:20
  • I forgot to push latest changes code. Older code I have but I have change many lines. – Avinash Dalvi Aug 14 '23 at 03:22
  • Which IDE do you use? If you're lucky enough, maybe the IDE has kept a local file history for you. See https://stackoverflow.com/questions/46446901/how-can-i-see-local-history-changes-in-visual-studio-code. – Zeke Lu Aug 14 '23 at 03:48
  • VSCode I used for code. – Avinash Dalvi Aug 14 '23 at 04:51
  • Get used to _never_ use filename arguments with the go tool. – Volker Aug 14 '23 at 09:47
  • 2
    That's what @blami said. Please take no offence, but it appears you do not have any clear idea about what that `go build -gcflags='all=-N -l' -o main.go` incantation really does, and that's the problem. If you did, you weren't be asking your question in the first place. Please don't mindlessly use stuff found on the 'net, always try to make sense of what you've found before using that. In this particular case, run `go help build` or see [this](https://pkg.go.dev/cmd/go#hdr-Compile_packages_and_dependencies) and learn what the `-o` command-line option does. – kostix Aug 14 '23 at 09:57

1 Answers1

2

The -o flag says "compile out to this file," so you overwrote your source with the compiled binary.

There are efforts to reverse engineer a binary, like the things mentioned here, but they're not going to get you back your exact code, or probably something even all that readable or complete, depending on how large your program was.

Use Git to track your code, and use a Makefile or some other build system (even a little batch script) so you don't have to type out the command every time.

Zac Anger
  • 6,983
  • 2
  • 15
  • 42
  • It means not way to get same code. I should be more careful while running this command with -o into main.go. – Avinash Dalvi Aug 14 '23 at 03:39
  • 1
    Correct. Use a Makefile, shell script, batch file, or whatever, so you can run something like `make build` or `./build.sh` rather than typing it out, and in there, don't put `-o main.go`, put `-o some-other-name` – Zac Anger Aug 14 '23 at 03:41