util

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2022 License: MIT Imports: 13 Imported by: 0

README

go-util

License GoDoc

go-util package contains some utility functions and types I often use every day.

Documentation

Overview

Package package contains some utility functions and types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddCookie

func AddCookie(r *http.Request, name, value string)

AddCookie adds a cookie to the HTTP request. AddCookie does not attach more than one Cookie header field. That means all cookies, if any, are written into the same line, separated by semicolon.

func CommaErrorFormatter

func CommaErrorFormatter(es []error) error

CommaErrorFormatter outputs errors separated by a comma. If there is only one error, returns the error itself.

func DelCookie

func DelCookie(r *http.Request, name string)

DelCookie deletes the cookies associated with the given name from the HTTP request's Cookie header.

func Errorf

func Errorf(code int, format string, args ...interface{}) error

Errorf formats according to a format specifier and returns the string as a value that satisfies error. Cause the underlying type of the error is *util.Error, you need to specify the error code (first parameter).

func GetClientIP

func GetClientIP(r *http.Request) string

GetClientIP gets the client ip from a http request. The implementation of this function based on some special HTTP headers, so it has security implications. Do NOT use this function unless there exists a trusted reverse proxy. The returned string might be empty or an illegal IP.

func GetCookie

func GetCookie(r *http.Request, name string) string

GetCookie gets the value of a cookie. If the named cookie is not found, returns an empty string. If multiple cookies match the given name, only one cookie value will be returned.

func GetLocalIP

func GetLocalIP() string

GetLocalIP returns local ip address.

func ListErrorFormatter

func ListErrorFormatter(es []error) error

ListErrorFormatter is a basic formatter that outputs the number of errors in the form of a list. If there is only one error, returns the error itself. If there're more than 16 errors, the remaining errors are indicated by ellipsis.

func SetCookie added in v0.1.4

func SetCookie(r *http.Request, name, value string)

SetCookie sets the named cookie to value. If the named cookie is not found, adds the new one.

func SetURLQuery added in v0.1.3

func SetURLQuery(URL string, query map[string]string) string

SetURLQuery injects query parameters into the URL and returns the modified one. If some query paramenters already exist, they will be updated.

func ToJson

func ToJson(v interface{}) []byte

ToJson returns the JSON encoding of v. Compared with json.Marshal, it won't escape special characters (&, <, and >) in quoted strings to avoid certain safety problems, and doesn't return error.

func ToPrettyJson

func ToPrettyJson(v interface{}) []byte

ToPrettyJson returns the pretty-print JSON encoding of v. It also won't escape special characters and doesn't return error.

func WrapError

func WrapError(code int, err error) error

WrapError wraps the raw error with an additional error code. The underlying type of the returned error is *util.Error.

Types

type BufferPool

type BufferPool struct {
	// contains filtered or unexported fields
}

BufferPool is a buffer pool which can reduce GC overhead.

func NewBufferPool

func NewBufferPool() *BufferPool

NewBufferPool creates a BufferPool instance.

func (*BufferPool) Get

func (bp *BufferPool) Get() *bytes.Buffer

Get fetches a buffer from the pool.

func (*BufferPool) Put

func (bp *BufferPool) Put(b *bytes.Buffer)

Put returns a buffer to the pool.

type BytesPool

type BytesPool struct {
	// contains filtered or unexported fields
}

BytesPool is an implementation of httputil.BufferPool interface.

func NewBytesPool

func NewBytesPool(size int) *BytesPool

NewBytesPool creates a BytesPool instance. The size parameter specifies the initial size of each generated byte slice, it must be positive.

func (*BytesPool) Get

func (bp *BytesPool) Get() []byte

Get fetches a byte slice from the pool.

func (*BytesPool) Put

func (bp *BytesPool) Put(b []byte)

Put returns a byte slice to the pool. Carefully, len(b) can't be zero, cause BytesPool will be used by io.CopyBuffer function.

type Error

type Error struct {

	// Code represents an error code that can help us to
	// locate a particular error quickly.
	Code int

	// File represents the file name of the source code
	// that generates the error.
	File string

	// Line represents the line number of the source code
	// at which the error occurs.
	Line int

	// Func represents the name of the function that generates the error.
	Func string
	// contains filtered or unexported fields
}

Error is an implementation of the error interface, and an additional error code is attached to it compared to the raw error. It's useful for us to distinguish error types accurately.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the underlying raw error.

type ErrorFormatter

type ErrorFormatter func([]error) error

ErrorFormatter specifies an interface that can convert the list of errors into an error.

type Group

type Group struct {

	// Logger specifies an optional logger for unexpected behaviors, which
	// lead to panic, from callback functions.
	Logger *log.Logger
	// contains filtered or unexported fields
}

Group is a collection of goroutines which usually run simultaneously.

func (*Group) Error

func (g *Group) Error(args ...interface{}) error

Error calls Result method at first, which means results will be cleared. Then it will extract all error results (the underlying type is error) and merge them into a single error. The message of the returned error is formatted by the first argument, the type of which should be ErrorFormatter. If you don't specify it or pass nil, ListErrorFormatter will be used. If there is no any error result, returns nil. In fact, if there are different types of return values, I don't recommend you use this method, cause it will ignore some non-error values.

func (*Group) Go

func (g *Group) Go(f func() interface{})

Go method is similar to 'go' statement which starts the execution of a function call in a new goroutine except for the function can't be arbitrary. The input function doesn't accept any parameter (but you can use the closure feature to inject parameters) and returns a value of any type. If the return value is (no type) nil, it will be ignored.

func (*Group) Result

func (g *Group) Result() []interface{}

Result method blocks until all function calls from the Go method have returned, then returns all resutls since the last time Result method was called, which means results will be cleared after calling this method.

Jump to

Keyboard shortcuts

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