lines

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 9, 2021 License: MIT Imports: 5 Imported by: 0

README ¶

🌈 lines

Package lines help working with lines of text

Documentation

Contributing

License

MIT Licensed

© 2021 Parro's Go Packages

Documentation ¶

Overview ¶

Package lines helps working with lines of text

Index ¶

Examples ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

func Func ¶

func Func() int

Func answers

func WriteFromChan ¶

func WriteFromChan(w io.Writer, c chan string) error

WriteFromChan continuously writes to w everything received from c chan.

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 ¶

func (lines *Buffer) AddLine(line string)

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 ¶

func (lines *Buffer) AddLineF(lineFormat string, arguments ...interface{})

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 ¶

func (lines *Buffer) Reset() []byte

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 ¶

func (lines *Buffer) Write(dest io.Writer) error

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 ¶

func (lines *Buffer) WriteFile(filepath string) error

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

Directories ¶

Path Synopsis
cli

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL