toolkit

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2018 License: MIT Imports: 6 Imported by: 0

README

toolkit

Collection of useful code snippets for Go.

Go Report Card Build Status GoDoc License

Includes
  • Drain(io.ReadCloser)

    This is particularly useful for the body of http.Response objects, where it is important to read the entire content of the body before closing it, so that the underlying connection can be reused. For example,

    resp, err := http.Get("google.com")
    if err != nil {...}
    defer toolkit.Drain(resp)
    
  • Interval(freq time.Duration, f Func) error

    This is useful for things which should be executed repeatedly at a certain frequency, including right now. This is basically the same thing as time.Tick, except it also executes immediately instead of starting executions after the wait period

    go toolkit.Interval(5 * time.Second, func() error {
        fmt.Println("hi")
        return nil
    })
    
  • Touch(filename string, mkdirs bool) (bool, error)

    This is similar to the unix touch command, where we just need to create a file and optionally any sub directories that are parents of that file.

    created, err := toolkit.Touch("/tmp/testdata/file.txt", true)
    

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Drain

func Drain(rc io.ReadCloser) (int64, error)

Drain will discard all content from a io.Reader and then Close it.

This is particularly useful for the body of http.Response objects, where it is important to read the entire content of the body before closing it, so that the underlying connection can be reused. For example,

resp, err := http.Get("google.com")
if err != nil {...}
defer resp.Drain(resp)

For more information read SO responses http://stackoverflow.com/questions/17948827/reusing-http-connections-in-golang

func Interval

func Interval(freq time.Duration, f Func) error

func Touch

func Touch(file string, mkdirs bool) (bool, error)

Touch will create an empty File named by file if it does not exist. If mkdirs is true, all intermediate directories will be created as well. Returns true if the file did not already exist and was createad.

Types

type Func

type Func func() error

Jump to

Keyboard shortcuts

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