Golang Minimal Version: 1.13
Package safe is a minimal project with safe methods to write clean code.
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")
// ...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)
// ...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) {
// ...
}
}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")
}Licensed under the MIT license, see LICENSE for details.