Documentation
¶
Overview ¶
Package package contains some utility functions and types.
Index ¶
- func AddCookie(r *http.Request, name, value string)
- func CommaErrorFormatter(es []error) error
- func DelCookie(r *http.Request, name string)
- func Errorf(code int, format string, args ...interface{}) error
- func GetClientIP(r *http.Request) string
- func GetCookie(r *http.Request, name string) string
- func GetLocalIP() string
- func ListErrorFormatter(es []error) error
- func SetCookie(r *http.Request, name, value string)
- func SetURLQuery(URL string, query map[string]string) string
- func ToJson(v interface{}) []byte
- func ToPrettyJson(v interface{}) []byte
- func WrapError(code int, err error) error
- type BufferPool
- type BytesPool
- type Error
- type ErrorFormatter
- type Group
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddCookie ¶
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 ¶
CommaErrorFormatter outputs errors separated by a comma. If there is only one error, returns the error itself.
func DelCookie ¶
DelCookie deletes the cookies associated with the given name from the HTTP request's Cookie header.
func Errorf ¶
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 ¶
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 ¶
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 ListErrorFormatter ¶
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
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
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.
Types ¶
type BufferPool ¶
type BufferPool struct {
// contains filtered or unexported fields
}
BufferPool is a buffer pool which can reduce GC overhead.
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 ¶
NewBytesPool creates a BytesPool instance. The size parameter specifies the initial size of each generated byte slice, it must be positive.
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.
type ErrorFormatter ¶
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 ¶
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.