Home > Software engineering >  Add fmt.Printf() lines to library installed via `go install ...`
Add fmt.Printf() lines to library installed via `go install ...`

Time:08-28

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/[email protected]/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?

CodePudding user response:

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.

CodePudding user response:

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
  •  Tags:  
  • go
  • Related