Skip to content

Unsafe try-catch solution for Go. It does not work on newer versions.

License

Notifications You must be signed in to change notification settings

LaevusDexter/unsafe-try-catch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

unsafe-try-catch

Unsafe try-catch solution for Go. It does not work on newer versions.

usage

func CopyFile(src, dst string) error {
	var tc, tcr TryCatcher
	var err error

	if err = tc.Catch(); err != nil {
		return fmt.Errorf("copy %s %s: %v", src, dst, err)
	}
	
	r, err := os.Open(src)
	tc.Try(err)

	defer r.Close()

	if err = tcr.Catch(); err != nil {
		os.Remove(dst)
		return fmt.Errorf("copy %s %s: %v", src, dst, err)
	}
	
	/* tc may be reused */
	if err = tc.Catch(); err != nil {
		w.Close()
		os.Remove(dst)
		return fmt.Errorf("copy %s %s: %v", src, dst, err)
	}

	w, err := check os.Create(dst)
	tcr.Try(err) // remove and return err

	_, err = io.Copy(w, r)
	tc.Try(err) // close, remove and return err

	err = w.Close()
	tcr.Try(err) // remove and return err

	return nil
}

About

Unsafe try-catch solution for Go. It does not work on newer versions.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages