5

I installed the go package browser.

Now the library does not work like I expect (issue) and I would like to add some fmt.Printf() lines to the source of the package.

If I modify the file via goland, I get a warning that no backup file could be created:

Cannot save /home/guettli/go/pkg/mod/github.com/pkg/browser@v0.0.0-20210911075715-681adbf594b8/browser.go. Unable to create a backup file (browser.go~). The file left unchanged.

How to add print-statements to third party code in go/goland?

Background: I am comming from Python, and since ages I add print-statements to third-party libraries, if I need to debug something.

guettli
  • 25,042
  • 81
  • 346
  • 663
  • Note that the language is called "Go". – Volker Aug 25 '22 at 06:44
  • 3
    What are you expecting from offering the bounty? As Volker said, you can't. Clone the module / package, add the changes, and use `replace` in `go.mod` to point to your local, changed version. What else do you wish for? – icza Aug 27 '22 at 17:16
  • 1
    @icza maybe there is an alternative solution? Maybe someone else has smart idea. – guettli Aug 31 '22 at 09:51
  • I don't understand the purpose of those print statements. Can't you just debug the code in different scenarios? If you clone the library and modify it it will work but you'll have to keep track of future changes and merge them into your clone. – cimere Aug 31 '22 at 15:18
  • Fork the project to your github account, do whatever changes you want to it, import it from your github account. – Bogdan Constantinescu Aug 31 '22 at 15:53

4 Answers4

6

How to add print-statements to third party code in [Go] [...]?

You cannot.

At least not in any simple way. You have to git (!) clone the module and replace the module in your go.mod to point to your clone. Change the clone.

Volker
  • 40,468
  • 7
  • 81
  • 87
5

Expanding on @Volker 's answer, you will need to have a local copy of the module to modify and use it. Here are the steps

  1. Clone the module repository git clone https://github.com/pkg/browser.git
  2. If needed, checkout to a branch/tag git checkout branch_name
  3. In the go.mod file of your module, add the following lines
replace (
    github.com/pkg/browser => /path/where/cloned/browser
)
  1. Now you should be able to modify code in the cloned browser repo and use it in your module
Suyash Medhavi
  • 1,135
  • 6
  • 18
1

Alternative approaches would be to use:

  • static analysis through ofabry/go-callvis library, to get a call graph and study which function calls what.
    call graph

  • dynamic/runtime analysis with pprof, as seen here, again to see which method does call what.
    https://i.stack.imgur.com/lmCos.jpg

In both instances, you do not need to modify the code source of your library.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

You definitely can change the source code of the third-party library. Just go to where the library is installed :

cd <GOPATH>/pkg/mod/github.com/pkg/

and then change the permissions of the folder browser@v0.0.0-20210911075715-681adbf594b8 to enable write :

chmod a+w browser@v0.0.0-20210911075715-681adbf594b8

and come back to GoLand, you are now able to save the file, because GoLand is able to save a backup inside the folder browser@v0.0.0-20210911075715-681adbf594b8 that had read-only permissions before you changed it.

And actually, if you read carefully what's inside the first Goland popup, it suggested to make changes to the whole folder containing the browser.go file the first time.

That being said, I'm also a Goland user, and never had to change the third party source code to debug it's behavior. All you have to do is set a breakpoint inside that third-party function and then debug your program as you would do normally. You will be able to explore the execution of the third party library code and dive deep inside it's functions calls and see the values of the variables and how the library behaves... Debugging is far more powerful than printing to the console.

Tourki
  • 1,785
  • 15
  • 22