将 fmt.Printf() 行添加到通过 `go install …` 安装的库中。

huangapple go评论73阅读模式
英文:

Add fmt.Printf() lines to library installed via `go install ...`

问题

我安装了Go包browser

现在这个库不像我期望的那样工作(问题链接),我想在该包的源代码中添加一些fmt.Printf()语句。

如果我通过Goland修改文件,会收到一个警告,无法创建备份文件:

> 无法保存 /home/guettli/go/pkg/mod/github.com/pkg/browser@v0.0.0-20210911075715-681adbf594b8/browser.go。
无法创建备份文件(browser.go~)。
文件未更改。

如何在Go/Goland中向第三方代码添加打印语句?

背景:我来自Python,很久以来,如果需要调试,我都会向第三方库添加打印语句。

英文:

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.

答案1

得分: 6

如何在第三方代码中添加打印语句(Go语言)?

你不能直接添加。

至少没有简单的方法。你需要使用git克隆该模块,并在你的go.mod文件中使用replace命令将该模块指向你克隆的版本。然后修改克隆版本的代码。

英文:

> 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.

答案2

得分: 5

根据@Volker的回答,你需要在本地拷贝该模块并进行修改和使用。以下是具体步骤:

  1. 克隆模块仓库:git clone https://github.com/pkg/browser.git
  2. 如果需要,切换到指定的分支或标签:git checkout branch_name
  3. 在你的模块的go.mod文件中添加以下内容:
replace (
	github.com/pkg/browser => /path/where/cloned/browser
)
  1. 现在你应该可以在克隆的browser仓库中修改代码,并在你的模块中使用它。
英文:

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

答案3

得分: 1

替代的方法是使用以下两种方式之一:

  • 静态分析,通过ofabry/go-callvis库获取调用图并研究哪个函数调用了哪个函数。
    将 fmt.Printf() 行添加到通过 `go install …` 安装的库中。

  • 动态/运行时分析,使用**pprof**,如这里所示,再次查看哪个方法调用了哪个函数。
    将 fmt.Printf() 行添加到通过 `go install …` 安装的库中。

在这两种情况下,您无需修改库的源代码。

英文:

Alternative approaches would be to use:

  • static analysis through ofabry/go-callvis library, to get a call graph and study which function calls what.
    将 fmt.Printf() 行添加到通过 `go install …` 安装的库中。

  • dynamic/runtime analysis with pprof, as seen here, again to see which method does call what.
    将 fmt.Printf() 行添加到通过 `go install …` 安装的库中。

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

答案4

得分: 0

你绝对可以更改第三方库的源代码。
只需进入库所安装的位置:

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

然后更改文件夹 browser@v0.0.0-20210911075715-681adbf594b8 的权限以启用写入:

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

然后回到 GoLand,现在你可以保存文件了,因为在你更改权限之前,GoLand 可以在文件夹 browser@v0.0.0-20210911075715-681adbf594b8 中保存备份,该文件夹之前是只读权限。

实际上,如果你仔细阅读第一个 Goland 弹出窗口中的内容,它建议对包含 browser.go 文件的整个文件夹进行更改。

也就是说,我也是 Goland 的用户,从来没有必要更改第三方源代码来调试其行为。你只需在第三方函数中设置断点,然后像平常一样调试程序。你将能够探索第三方库代码的执行过程,深入了解其函数调用的值以及库的行为... 调试比在控制台打印信息更强大。

英文:

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

cd &lt;GOPATH&gt;/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.

huangapple
  • 本文由 发表于 2022年8月25日 14:36:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/73482938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定