Documentation ¶
Overview ¶
Package lines helps working with lines of text
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer contains a bytes.Buffer and simplify writing lines of text to it.
The empty value of a Buffer is already usable.
You can recycle Buffer multiple times, any call to WriteFile will truncate the underlying bytes.Buffer to 0.
If you want to start from scratch without having to write to a file, you can call Discard method.
func (*Buffer) AddLine ¶
AddLine writes a line of text in the buffer. A newline is appended at the end of the strigns only if it's not already present.
func (*Buffer) AddLineF ¶
AddLineF writes a line of text in the buffer. The line to append is built calling `fmt.Sprintf` with `lineFormat` and `arguments...`
func (*Buffer) Reset ¶
Reset truncates the internal lines buffer, so that you can recycle the same Buffer as if it was newly created. Current content of the buffer is returned as a `[]byte`
func (*Buffer) Write ¶
Write writes all lines written to the buffer so far in a io.Writer.
If if the write operation succeed, Write truncates the internal lines buffer, so that you can recycle the same Buffer as if it was newly created.
If an error occurrs during the writing, no truncation of the buffer is done, so that you can retry the method call later.
Example ¶
This example show how to use a lines.Buffer to acccumulate a bunch of lines and write them to console at end using `Write` method.
package main import ( "os" "github.com/parrogo/lines" ) func main() { var lines lines.Buffer lines.AddLineF("A simple line") lines.AddLineF("Trailing new lines are not needed\n") lines.AddLineF("You can use %s arguments", "fmt.Printf") err := lines.Write(os.Stdout) if err != nil { panic(err) } }
Output: A simple line Trailing new lines are not needed You can use fmt.Printf arguments
func (*Buffer) WriteFile ¶
WriteFile writes all lines written to the buffer so far to a file with given path.
After the file is written successfully, WriteFile truncates the internal lines buffer, so that you can recycle the same Buffer as if it was newly created.
If an error occurrs during the writing of the file, no truncation of the buffer is done, so that you can retry the method call later.
Example ¶
This example show how to use a lines.Buffer
package main import ( "fmt" "os" "github.com/parrogo/lines" ) func main() { var lines lines.Buffer lines.AddLineF("A simple line") lines.AddLineF("Trailing new lines are not needed\n") lines.AddLineF("You can use %s arguments", "fmt.Printf") err := lines.WriteFile("/tmp/example") if err != nil { panic(err) } // read the content of the file just written content, _ := os.ReadFile("/tmp/example") os.Remove("/tmp/example") fmt.Print(string(content)) }
Output: A simple line Trailing new lines are not needed You can use fmt.Printf arguments