修改Golang内部文件?

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

Modify Golang internal file?

问题

你好!根据你的描述,你想通过修改go/src目录下的源文件来实现你的想法,但是遇到了错误。要解决这个问题,你可以按照以下步骤进行操作:

  1. 首先,确保你有足够的权限来修改go/src目录下的文件。

  2. 在修改任何文件之前,建议先备份原始文件,以防止意外情况发生。

  3. 在你的main.go文件中,将import sync改为import ./sync,这样可以确保导入的是你修改后的sync包。

  4. go/src/sync文件夹复制到包含main.go文件的桌面文件夹中。

  5. go/src/internal文件夹复制到包含main.go文件的桌面文件夹中。

  6. 修改./sync文件夹中的所有文件,将引用internal/..的地方改为./internal/..

  7. 运行go run main.go命令,查看是否还有错误消息。

请注意,修改go/src目录下的源文件可能会导致不可预料的问题,建议在进行修改之前先备份文件,并谨慎操作。另外,修改标准库的源代码可能会导致与其他代码库的兼容性问题,需要谨慎考虑。

英文:

I had the idea of adding a sync.WaitGroup Limit(max int) function to resrict the number of the WaitGroup counter.

So I opened the waitgroup.go file in go/src/sync and made the changes, saved the file and tried testing it in a main.go file on my desktop. When I ran the file, it says:

  1. $ go run main.go
  2. wg.Limit undefined (type sync.WaitGroup has no field or method Limit)

To fix this error message, I copied the folder from go/src/sync into the folder on my desktop containing my main.go file and changed the import from sync to ./sync.

After running go run main.go this time, I got the following output:

  1. $ go run main.go
  2. sync\mutex.go:14:2: use of internal package not allowed
  3. sync\pool.go:8:2: use of internal package not allowed
  4. sync\rwmutex.go:8:2: use of internal package not allowed
  5. sync\waitgroup.go:8:2: use of internal package not allowed

To fix these messages, I copied go/src/internal into the folder on my desktop containing my main.go file and I modified all the files in ./sync that reference internal/.. to ./internal/..

I run it again and I get the following output:

  1. $ go run main.go
  2. # _/C_/.../sync
  3. sync\mutex.go:20: missing function body for "throw"
  4. sync\pool.go:66: missing function body for "fastrand"
  5. sync\pool.go:249: missing function body for "runtime_registerPoolCleanup"
  6. sync\pool.go:250: missing function body for "runtime_procPin"
  7. sync\pool.go:251: missing function body for "runtime_procUnpin"
  8. sync\runtime.go:14: missing function body for "runtime_Semacquire"
  9. sync\runtime.go:17: missing function body for "runtime_SemacquireMutex"
  10. sync\runtime.go:23: missing function body for "runtime_Semrelease"
  11. sync\runtime.go:36: missing function body for "runtime_notifyListAdd"
  12. sync\runtime.go:39: missing function body for "runtime_notifyListWait"
  13. sync\runtime.go:39: too many errors

How can I implement my simple idea by modifying the source files of go/src without receiving these errors?

答案1

得分: 0

在您安装的Go中,导航到src文件夹,并运行名为run.bat的文件,它将重新编译所有的包,从而消除您所描述的第一个错误。

英文:

In your installation of Go, navigate to the src folder and run the file called run.bat which will recompile all the packages removing the first error you described.

答案2

得分: 0

我真的不会通过更改标准库来实现这个,而是将WaitGroup封装起来,以便跟踪计数器,像这样:

  1. type wg_limit struct {
  2. wg sync.WaitGroup
  3. current, max int
  4. }
  5. func (wgl *wg_limit) Add(delta int) error {
  6. if wgl.current+delta > wgl.max {
  7. return fmt.Errorf("计数器超过限制(当前:%d,最大:%d)", wgl.current, wgl.max)
  8. }
  9. wgl.current += delta
  10. wgl.wg.Add(delta)
  11. return nil
  12. }
  13. func (wgl *wg_limit) Done() {
  14. wgl.current -= 1
  15. wgl.wg.Done()
  16. }

然后,使用一个max值初始化wg_limit,并像使用WaitGroup一样使用结果变量:

  1. wgl := wg_limit{max: 3}
  2. if err := wgl.Add(1); err != nil {
  3. // 做一些操作()
  4. // ...
  5. wgl.Done()
  6. }

如果Add(delta)的结果会导致WaitGroup计数器超过限制,将返回一个错误,并且WaitGroup不会改变。

英文:

I really wouldn't do this by changing the standard library, but instead wrap the WaitGroup to keep track of the counter like so:

  1. type wg_limit struct {
  2. wg sync.WaitGroup
  3. current, max int
  4. }
  5. func (wgl *wg_limit) Add(delta int) error {
  6. if wgl.current+delta > wgl.max {
  7. return fmt.Errorf("counter exceeded (current: %d, max: %d)", wgl.current, wgl.max)
  8. }
  9. wgl.current += delta
  10. wgl.wg.Add(delta)
  11. return nil
  12. }
  13. func (wgl *wg_limit) Done() {
  14. wgl.current -= 1
  15. wgl.wg.Done()
  16. }

Then init wg_limit with a value for max and use the resulting variable just like a WaitGroup:

  1. wgl := wg_limit{max: 3}
  2. if err := wgl.Add(1); err != nil {
  3. // go something()
  4. // ...
  5. wgl.Done()
  6. }

If the WaitGroup counter would be exceeded by the delta of Add(delta), an error is returned and the WaitGroup is unchanged.

huangapple
  • 本文由 发表于 2017年9月16日 22:09:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/46254790.html
匿名

发表评论

匿名网友

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

确定