So how do we capture print inside a void function that has no "writer" parameter?
func SayHello(name string) { fmt.Print("Hi, my name is", name) }
You can do it with example function from testing package:
testing
func ExampleSayHello() { fmt.Println("Alex") // Output: Hi, my name is Alex" }
(btw, the result may be unexpected for your function: go.dev/play/p/J4zY09qEU4X )
Or use os.Pipe() as described in this discussion, but be aware of buffer overflows and data race.
os.Pipe()
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
So how do we capture print inside a void function that has no "writer" parameter?
func SayHello(name string) {
fmt.Print("Hi, my name is", name)
}
You can do it with example function from
testing
package:(btw, the result may be unexpected for your function: go.dev/play/p/J4zY09qEU4X )
Or use
os.Pipe()
as described in this discussion, but be aware of buffer overflows and data race.