如何获取并发方法

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

How to get a concurrent method

问题

如何获得一个并发的方法?

type test struct {
	foo uint8
	bar uint8
}

func NewTest(arg1 string) (*test, os.Error) {...}

func (self *test) Get(str string) ([]byte, os.Error) {...}

我认为所有Get()方法的代码都应该放在go func()内部,然后使用一个通道。

func (self *test) Get(str string) ([]byte, os.Error) {
	go func() {
		// 这个方法的代码。
	}()
}
  • 如果从Get()方法调用另一个方法,会有问题吗?还是它也必须是并发的?
英文:

How to get a concurrent method?

type test struct {
	foo uint8
	bar uint8
}

func NewTest(arg1 string) (*test, os.Error) {...}

func (self *test) Get(str string) ([]byte, os.Error) {...}

I think that all code for method Get() should be put inner of go func(), and then to use a channel.

func (self *test) Get(str string) ([]byte, os.Error) {
	go func() {
		// Code for this method.
	}()
}
  • Would there be a problem if it's called another method from Get()? Or would it also has to be concurrent?

答案1

得分: 1

在Go语言中实现并发的方式是在你想要同时执行的函数前面使用关键字"go":

func bar() { ... }

func foo() {
go bar()
go bar()
go bar()
... 等待所有bar函数执行完的代码 ...
}

这就是同时运行3个bar函数所需的全部。PS:你可能希望提供一个你熟悉的语言(如C++ / Perl / Python / 其他),以示例的形式,因为你提供的Go代码片段并没有太大帮助。我意识到你可能比我更擅长英语,但你可能还想得到一些帮助,以更清楚地表达你的问题。

Hotei

英文:

The way you get concurrency in go is to use the keyword "go" in front of functions you want to execute concurrently:

func bar () { ... }

func foo () {
go bar()
go bar()
go bar()
... code that waits for all the bars to close ...
}

That's all it takes to run 3 bars at the same time. PS: You might want to provide an example in a language you are familiar with, C++ / Perl / Python / whatever since the go code fragment you included wasn't much help. I realize you probably speak English better than I speak your native language, but you might also want to get some assistance formulating your question more clearly.

Hotei

答案2

得分: 1

请查看《Go语言规范》中的示例包部分,这是一个完整的Go包,实现了一个使用go语句和通道的并发素数筛。

有关其工作原理的详细描述,请参阅《Go教程》中的素数部分。还可以查看《Go教程》中的多路复用部分。

请阅读《Effective Go》中的并发部分。

最后,请阅读《Go语言规范》中的相关部分,例如Go语句通道类型选择语句的部分。

是的,您可以从Get()方法中调用另一个方法。由于方法调用不是并发的go语句,它将立即执行,在执行下一条语句之前。

英文:

Take a look at the An example package section in The Go Language Specification, which is a complete Go package that implements a concurrent prime sieve, using go statements and channels.

For a detailed description of how it works, see the Go Tutorial section on Prime numbers. Also, look at the Go Tutorial section on Multiplexing.

Read the Effective Go section on Concurrency.

Finally, read the relevant sections of The Go Language Specification e.g. the sections on Go statements, Channel types, and Select statements.

Yes, you can call another method from your Get() method. Since a method call is not a concurrent go statement, it will execute immediately, before executing the next statement.

huangapple
  • 本文由 发表于 2010年5月5日 17:33:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/2771978.html
匿名

发表评论

匿名网友

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

确定