如何在标准库或第三方中正确使用Golang包与Goroutines?

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

How to properly use Golang packages in the standard library or third-party with Goroutines?

问题

嗨,Golang程序员们,

首先,如果我的问题一开始不太清楚,我向你们道歉,但我正在努力理解在编写使用标准库或其他库时使用Goroutines的Golang代码时的正确使用模式。

让我详细说明一下:假设我导入了一些我没有参与编写的包,我想要利用它。假设这个包以某种方式对像Flickr这样的网站进行了简单的http get请求。如果我想要并发请求,我可以在函数调用前加上go关键字。但是,我如何知道,当这个包进行请求时,它是否已经自己做了一些内部的go调用,从而使我的go调用变得多余?

Golang包通常在文档中说明他们的方法是否是“greened”吗?或者他们提供两个版本的方法,一个是green的,一个是直接的同步方法?

在我努力理解Go的习惯用法和使用模式时,我觉得即使在使用标准库中的包时,我也不能确定我的go命令是否是必要的。我想我可以对调用进行性能分析,或者编写测试代码,但这感觉很奇怪,因为我必须弄清楚一个函数是否已经是“green”的。

我想另一个可能性是,我需要研究我正在使用的任何东西的源代码,并理解它应该如何使用,以及go关键字是否是必要的。

如果有人能给我一些启示,或者指导我正确的文档,甚至是一个Golang的屏幕录像,我将非常感激。我想Rob Pike在一个演讲中简要提到过,一个用Go编写的良好的客户端API只需以典型的同步方式编写,由调用者选择是否将其变为green。

感谢你们的时间,

-Ralph

英文:

Hi Golang programmers,

First of all I apologize if my question is not very clear initially but I'm trying to understand the proper usage pattern when writing Golang code that uses Goroutines when using the standard lib or other libraries.

Let me elaborate: Suppose I import some package that I didn't have a hand in writing that I want to utilize. Let's say this package does a simple http get request somehow to a website such as Flickr for example. If I want a concurrent request, I can just prefix the function call with the go keyword. But how do I know, that this package when doing the request doesn't already do some internal go calls itself therefore making my go calls redundant?

Do Golang packages typically say in the documentation that their method is "greened"? Or perhaps they provide two versions of a method, one that is green and one that is straight synchronous?

In my quest to understand Go idioms and usage patterns I feel like when using even packages in the standard lib that I can't be sure if my go commands are necessary. I suppose I can profile the calls, or write test code but that feels odd to have to figure out if a func is already "green".

I suppose another possibility is that it's up to me to study the source code of whatever I'm using and understand how it should be used and if the go keyword is necessary.

If anybody can shed some light on this or point me to the right documentation or even a Golang screen-cast I'd much appreciate it. I think Rob Pike briefly mentions in one talk that a good client api written go is just written in a typical synchronous manner and it's up to the caller of that api to have the choice of making it green or not.

Thanks for your time,

-Ralph

答案1

得分: 3

如果一个函数/方法返回一些值,或者有类似于io.Reader.Read的副作用 - 那么它必然是同步的。除非有明确的文档说明,否则不能假设它可以安全地被多个goroutine并发使用。

如果它接受一个闭包(回调)或一个通道,或者它返回一个通道 - 那么它通常是一个异步的东西。如果是这种情况,通常要么显而易见,要么明确地在文档中说明。这种异步的东西通常可以安全地被多个goroutine并发使用。

英文:

If a function / method returns some value(s), or have a side effect like that (io.Reader.Read) - then it's necessarily a synchronous thing. Unless documented otherwise, no safety for concurrent use by multiple goroutines should be assumed.

If it accepts a closure (callback) or a channel or if it returns a channel - then it is often an asynchronous thing. If that's the case, it's normally either obvious or explicitly documented. Asynchronous stuff like this is usually safe for concurrent use by multiple goroutines.

huangapple
  • 本文由 发表于 2013年6月6日 09:22:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/16952310.html
匿名

发表评论

匿名网友

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

确定