In Go, how do I capture stdout of a function into a string?
问题 In Python, for example, I can do the following: realout = sys.stdout sys.stdout = StringIO.StringIO() some_function() # prints to stdout get captured in the StringIO object result = sys.stdout.getvalue() sys.stdout = realout Can you do this in Go? 回答1: I agree you should use the fmt.Fprint functions if you can manage it. However, if you don't control the code whose output you're capturing, you may not have that option. Mostafa's answer works, but if you want to do it without a temporary file