问题描述:

I read the code of exec.Start,and there some part confusing me. (*Cmd).stdin/out/err in the []F,what's the meaning of (*Cmd).stdXX?

 291 type F func(*Cmd) (*os.File, error)

292 for _, setupFd := range []F{(*Cmd).stdin, (*Cmd).stdout, (*Cmd).stderr} {

293 fd, err := setupFd(c)

294 if err != nil {

295 c.closeDescriptors(c.closeAfterStart)

296 c.closeDescriptors(c.closeAfterWait)

297 return err

298 }

299 c.childFiles = append(c.childFiles, fd)

300 }

网友答案:

Very Nice find, I did not know that idiom before. I will try to break it down. First We have exec.Cmd

type exec.Cmd struct { ... }

*Cmd has 3 many methods including stdin stdout stderr

func (c *Cmd) stdin() (f *os.File, err error) {...}
func (c *Cmd) stdout() (f *os.File, err error) {...}
func (c *Cmd) stderr() (f *os.File, err error) {...}

Now I want to call all these functions and do the same set of operations on each of them but I don't want to create another method because there are too many shared variable to pass by arguments.

The first solution would be to just copy / paste the code 3 times. not nice.

The second is to loop over an array of Functors. The Functor type would be func(c*Cmd)(f *os.File, err error) so we declare it as

type F (c *Cmd) (f *os.File, err error)

now we can create the array of functors. But how to select a *Cmd method ? simply using

(*Cmd).<name of method>

So it would be (*Cmd).stdin, (*Cmd.stdout), (*Cmd).stderr and we can use them as An array

[]F{ (*Cmd).stdin, (*Cmd.stdout), (*Cmd).stderr }

we just need to call them now

for _, setupFd := range []F{(*Cmd).stdin, (*Cmd).stdout, (*Cmd).stderr} {
    fd, err := setupFd(c)
    ...
}

Hope this helps.

网友答案:

A very interesting idiom I have never encountered before. The code takes *Cmd's methods as functions and calls them in order on c. Here is a simpler code example that shows how it works: http://play.golang.org/p/XwuYD_9uGs.

That code could as well be written as for body called with three different stdXX, but that would be repetitive and bug-prone, so the author decided instead to apply three methods in a cycle.

相关阅读:
Top