summaryrefslogtreecommitdiff
path: root/fakeStdout.go
diff options
context:
space:
mode:
Diffstat (limited to 'fakeStdout.go')
-rw-r--r--fakeStdout.go79
1 files changed, 79 insertions, 0 deletions
diff --git a/fakeStdout.go b/fakeStdout.go
new file mode 100644
index 0000000..b263f8a
--- /dev/null
+++ b/fakeStdout.go
@@ -0,0 +1,79 @@
+package argvpb
+
+// StringWriter is a custom type that implements the io.Writer interface
+// for a string variable. It holds a pointer to the string it will write to.
+type StringWriter struct {
+ s *string
+}
+
+// NewStringWriter creates a new StringWriter that writes to the provided string pointer.
+func NewStringWriter(s *string) *StringWriter {
+ return &StringWriter{s: s}
+}
+
+// Write implements the io.Writer interface. It appends the byte slice `p`
+// to the string pointed to by the StringWriter.
+func (sw *StringWriter) Write(p []byte) (n int, err error) {
+ // Append the contents of the byte slice p to the string *sw.s
+ *sw.s += string(p)
+ return len(p), nil
+}
+
+var Stdout *StringWriter
+var Stderr *StringWriter
+
+func fakeStdout() {
+ Stdout = NewStringWriter(&PB.Stdout)
+ Stderr = NewStringWriter(&PB.Stderr)
+}
+
+/*
+func main() {
+ // 1. Declare the string variable that will be our output destination.
+ var myout string
+
+ // 2. Create a new writer that points to our string variable.
+ // Our StringWriter now satisfies the io.Writer interface.
+ writer := NewStringWriter(&myout)
+
+ // 3. Use our writer with functions that accept an io.Writer.
+ fmt.Fprintln(writer, "Hello, world!")
+ io.WriteString(writer, "This is the second line.\n")
+ fmt.Fprintf(writer, "A number: %d\n", 123)
+
+ // 4. Print the final contents of the string to verify.
+ fmt.Println("--- Final content of myout: ---")
+ fmt.Print(myout)
+ fmt.Println("-----------------------------")
+}
+*/
+/*
+package main
+
+import (
+ "fmt"
+ "io"
+ "strings"
+)
+
+func main() {
+ // 1. Declare the final string variable.
+ var myout string
+
+ // 2. Create a strings.Builder. It already implements io.Writer.
+ var builder strings.Builder
+
+ // 3. Use the builder as the io.Writer.
+ fmt.Fprintln(&builder, "Hello from the builder!")
+ io.WriteString(&builder, "This is the second line.\n")
+ fmt.Fprintf(&builder, "Another number: %d\n", 456)
+
+ // 4. Get the final string from the builder and assign it.
+ myout = builder.String()
+
+ // 5. Print the final contents of the string to verify.
+ fmt.Println("--- Final content of myout: ---")
+ fmt.Print(myout)
+ fmt.Println("-----------------------------")
+}
+*/