Documentation ¶
Overview ¶
Package util implements small helper function that should be included in the stdlib in our opinion.
Index ¶
- Variables
- func Clamp(x, lo, hi int) int
- func Closer(c io.Closer)
- func DeriveKey(pwd, salt []byte, keyLen int) []byte
- func LimitWriter(w io.Writer, sz int64) io.Writer
- func Max(a, b int) int
- func Max64(a, b int64) int64
- func Min(a, b int) int
- func Min64(a, b int64) int64
- func NewTimeoutReader(r io.Reader, d time.Duration) io.Reader
- func NewTimeoutWriter(w io.Writer, d time.Duration) io.Writer
- func NopWriteCloser(w io.Writer) io.WriteCloser
- func OmitBytes(data []byte, lim int) string
- func PrefixReader(data []byte, r io.Reader) io.Reader
- func SyncedReadWriter(w io.ReadWriter) io.ReadWriter
- func Tar(root, archiveName string, w io.Writer) error
- func Touch(path string) error
- func UClamp(x, lo, hi uint) uint
- func UMax(a, b uint) uint
- func UMin(a, b uint) uint
- func Untar(r io.Reader, root string) error
- type Empty
- type Errors
- type SizeAccumulator
- type SyncBuffer
- type TimeoutReadWriter
- func (rw *TimeoutReadWriter) Read(p []byte) (n int, err error)
- func (rw *TimeoutReadWriter) SetDeadline(d time.Time) error
- func (rw *TimeoutReadWriter) SetReadDeadline(d time.Time) error
- func (rw *TimeoutReadWriter) SetReadTimeout(d time.Duration) error
- func (rw *TimeoutReadWriter) SetTimeout(d time.Duration) error
- func (rw *TimeoutReadWriter) SetWriteDeadline(d time.Time) error
- func (rw *TimeoutReadWriter) SetWriteTimeout(d time.Duration) error
- func (rw *TimeoutReadWriter) Write(p []byte) (n int, err error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrTimeout = errors.New("I/O Timeout: Operation timed out")
Functions ¶
func Closer ¶
Closer closes c. If that fails, it will log the error. The intended usage is for convinient defer calls only! It gives only little knowledge about where the error is, but it's slightly better than a bare defer xyz.Close()
func DeriveKey ¶
DeriveKey derives a key from password and salt being keyLen bytes long. It uses an established password derivation function.
func NewTimeoutWriter ¶
TimeoutReadWriter wraps `w` and returns a io.Writer that times out after `d` elapsed with ErrTimeout if `w` didn't succeed in that time.
func NopWriteCloser ¶
func NopWriteCloser(w io.Writer) io.WriteCloser
NopCloser returns a WriteCloser with a no-op Close method wrapping the provided Writer w.
func OmitBytes ¶
OmitBytes converts a byte slice into a string representation that omits data in the middle if necessary. It is useful for testing and printing user information. `lim` is the number of bytes
Example:
OmitBytes([]byte{1,2,3,4}, 2) -> [1 ... 2] OmitBytes([]byte{1,2,3,4}, 4) -> [1, 2, 3, 4]
func SyncedReadWriter ¶
func SyncedReadWriter(w io.ReadWriter) io.ReadWriter
SyncedReadWriter returns a io.ReadWriter that protects each call to Read() and Write() with a sync.Mutex.
Types ¶
type SizeAccumulator ¶
type SizeAccumulator struct {
// contains filtered or unexported fields
}
SizeAccumulator is a io.Writer that simply counts the amount of bytes that has been written to it. It's useful to count the received bytes from a reader in conjunction with a io.TeeReader
Example usage without error handling:
s := &SizeAccumulator{} teeR := io.TeeReader(r, s) io.Copy(os.Stdout, teeR) fmt.Printf("Wrote %d bytes to stdout\n", s.Size())
Example ¶
s := &SizeAccumulator{} teeR := io.TeeReader(bytes.NewReader([]byte("Hello, ")), s) io.Copy(os.Stdout, teeR) fmt.Printf("wrote %d bytes to stdout\n", s.Size())
Output: Hello, wrote 7 bytes to stdout
func (*SizeAccumulator) Reset ¶
func (s *SizeAccumulator) Reset()
Reset resets the size counter to 0.
func (*SizeAccumulator) Size ¶
func (s *SizeAccumulator) Size() uint64
Size returns the cumulated written bytes. It can be safely called from any go routine.
type SyncBuffer ¶
SyncBuffer is a bytes.Buffer that protects each call to Read() and Write() with a sync.RWMutex, i.e. parallel access to Read() is possible, but blocks when doing a Write().
type TimeoutReadWriter ¶
func NewTimeoutReadWriter ¶
func NewTimeoutReadWriter(rw io.ReadWriter, d time.Duration) *TimeoutReadWriter
func (*TimeoutReadWriter) SetDeadline ¶
func (rw *TimeoutReadWriter) SetDeadline(d time.Time) error
func (*TimeoutReadWriter) SetReadDeadline ¶
func (rw *TimeoutReadWriter) SetReadDeadline(d time.Time) error
func (*TimeoutReadWriter) SetReadTimeout ¶
func (rw *TimeoutReadWriter) SetReadTimeout(d time.Duration) error
func (*TimeoutReadWriter) SetTimeout ¶
func (rw *TimeoutReadWriter) SetTimeout(d time.Duration) error
func (*TimeoutReadWriter) SetWriteDeadline ¶
func (rw *TimeoutReadWriter) SetWriteDeadline(d time.Time) error
func (*TimeoutReadWriter) SetWriteTimeout ¶
func (rw *TimeoutReadWriter) SetWriteTimeout(d time.Duration) error
Directories ¶
Path | Synopsis |
---|---|
Package conductor is a small helper to execute work heavy operations in the backgrounds that deliver partial results ("result streaming").
|
Package conductor is a small helper to execute work heavy operations in the backgrounds that deliver partial results ("result streaming"). |
Package log implements utility methods for logging in a colorful manner.
|
Package log implements utility methods for logging in a colorful manner. |
Package trie implements a general purpose Path-*Node.
|
Package trie implements a general purpose Path-*Node. |