Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

ayasechan/go-result

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-result

Result and Option for golang

Install

go get github.com/ayasechan/go-result

Example

func ExampleResult_downloadFile() {
	download := func(url string, dst io.Writer) Result[struct{}] {
		return WithRecover(func() Result[struct{}] {
			req := AsResult(http.NewRequest(http.MethodGet, url, nil)).Unwrap()
			resp := AsResult(http.DefaultClient.Do(req)).Unwrap()
			defer resp.Body.Close()

			AsResult(io.Copy(dst, resp.Body)).Unwrap()
			return Ok(struct{}{})
		})
	}

	dstFd := AsResult(os.CreateTemp("", ".temp")).Unwrap()
	defer dstFd.Close()
	defer os.Remove(dstFd.Name())

	download("https://httpbin.org/", dstFd).InspectErr(func(err error) {
		fmt.Printf("%+v", err)
		os.Exit(1)
	})
}