Skip to content

Latest commit

 

History

History
59 lines (40 loc) · 1.73 KB

README.md

File metadata and controls

59 lines (40 loc) · 1.73 KB

safe

Build Status Go Report Card GoDoc Coverage Status

Golang Minimal Version: 1.13

Package safe is a minimal project with safe methods to write clean code.

safe.Close

Close will close any io.Closer object if it's not nil, any error will be only logged.

	resp, _ := http.DefaultClient.Get("http://example.com")
	defer safe.Close(resp.Body, "GET http://example.com")
	// ...

safe.Must

Must will return the first argument as the result, if the error is nil, otherwise will call panic.

	resp := safe.Must(http.DefaultClient.Get("http://example.com")).(http.*Response)
	// ...

safe.IsNil

IsNil is a safe and fast method to check is current interface{} value is nil.

Reason is incident, that nil != nil in some cases.

Full description of the problem in article "Go-tcha: When nil != nil".

func Foo(str fmt.Stringer) {
    if !safe.IsNil(str) {
        // ...
    }
}

safe.Wrap

Wrap will wrap current error with the scope value, or return nil if error wasn't set.

func Unmarshal(data []byte) (value interface{}, err error) {
	return value, safe.Wrap(json.Unmarshal(data, &value), "broken json")
}

License

Licensed under the MIT license, see LICENSE for details.