Documentation ¶
Overview ¶
Package linesbuf implements a buffer of in memory string.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type LinesBuf ¶
type LinesBuf struct {
// contains filtered or unexported fields
}
LinesBuf contains a bytes.Buffer and simplify writing lines of text to it.
The empty value of a LinesBuf is already usable.
You can recycle LinesBuf 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.
Example ¶
This example show how to use a linesbuf.LinesBuf
package main import ( "embed" "io/fs" "os" "github.com/parro-it/linesbuf" ) //go:embed fixtures var fixtureRootFS embed.FS var fixtureFS, _ = fs.Sub(fixtureRootFS, "fixtures") // This example show how to use a linesbuf.LinesBuf func main() { var lines linesbuf.LinesBuf lines.AddLine("A simple line") lines.AddLine("Trailing new lines are not needed\n") lines.AddLine("You can use %s arguments", "fmt.Printf") lines.Write(os.Stdout) }
Output: A simple line Trailing new lines are not needed You can use fmt.Printf arguments
func (*LinesBuf) AddLine ¶
AddLine writes a line of text in the buffer. The line to append is built calling `fmt.Sprintf` with `lineFormat` and `arguments...`
func (*LinesBuf) Discard ¶
func (lines *LinesBuf) Discard()
Discard truncates the lines buffer, so that you can recycle the same LinesBuf as if it was newly created.
func (*LinesBuf) 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 LinesBuf 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.
func (*LinesBuf) 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 LinesBuf 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.