Skip to content
/ safe Public

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

License

Notifications You must be signed in to change notification settings

spyzhov/safe

Repository files navigation

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.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages