closer

package
v1.0.10023-a10e065 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package closer contains a helper function for not losing deferred errors

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorHandler

func ErrorHandler(c io.Closer, in *error)
Example
package main

import (
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"
	"os"

	"github.com/circleci/ex/closer"
)

func main() {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		_, _ = io.WriteString(w, "Hello world!")
	}))
	defer srv.Close()

	output, err := run(srv.URL)
	if err != nil {
		os.Exit(1)
	}
	fmt.Println(output)

}

func run(rawurl string) (_ string, err error) {
	//#nosec:G107 // this is a test
	//nolint:bodyclose // handled by closer
	resp, err := http.Get(rawurl)
	if err != nil {
		return "", err
	}
	defer closer.ErrorHandler(resp.Body, &err)

	b, err := io.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}

	return string(b), nil
}
Output:

Hello world!

Types

This section is empty.

Jump to

Keyboard shortcuts

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