douxing6532 2010-05-05 09:33
浏览 22
已采纳

如何获得并发方法

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?
  • 写回答

2条回答 默认 最新

  • dongzhuo7291 2010-05-07 16:56
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?