My (clearly flawed) interpretation of interface types suggested that I could assign the first result from c.StdoutPipe() to anything that implements io.ReadCloser.
The io.PipeReader documentation indicates that it provides both a Read() and a Close() function with the appropriate signatures, so I figured I’d try connecting up both ends of a pipe:rPipe, _ := io.Pipe()p, err := cmd.StdoutPipe()if err != nil {log.Fatal(err)}rPipe = pThe first clue that I was misguided was when the compiler indicated that I needed a type assertion. I put one in to see what would happen:rPipe = p.(*io.PipeReader)Everything compiled, but not surprising, it panics:panic: interface conversion: io.ReadCloser is *os.File, not *io.PipeReaderI suppose it makes sense that an assignment operator requires more than simply interface compatibility. A couple of questions:
- How is one supposed to know what type is required in a situation like this? Is a type assertion panic the only way to discover that a *os.File type is required?!
- Any suggestions on how I can get a io.PipeReader to connect with the cmd.StdoutPipe()?
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/d7c51270-5e2c-439d-a1bb-d78fd65a6a25n%40googlegroups.com.
The example uses a “short assignment” statement, so it’s not obvious what “concrete” type c.StdoutPipe() returns
On Sunday, 28 February 2021 at 03:03:17 UTC Deiter wrote:The example uses a “short assignment” statement, so it’s not obvious what “concrete” type c.StdoutPipe() returnsYou can't tell
Thanks Brian, Axel & Ian for your very thorough treatment of my post.
When I first read about golang interfaces, I was impressed with how elegant and powerful they are, so it's very disappointing that my first opportunity at leveraging them was such a fail! Your explanations did a remarkable job of making proper usage obvious. I appreciate you taking the time.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/b003d635-0069-4a22-9367-db9db907a672n%40googlegroups.com.