Search code examples
methodsgogoroutine

How to get a concurrent method


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?

Solution

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