Skip to content

dankinder/handle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

handle

Handle go(golang) errors in simple scripts with an easy one-liner. Package handle provides a dead-simple function to panic on error. Not for use in production software where the typical method of check if err != nil is the right thing to do.

Writing Go involves many error checks:

func CopyFile(dstName, srcName string) {
	src, err := os.Open(srcName)
	if err != nil {
		// Handle this somehow...
	}
	defer src.Close()

	dst, err := os.Create(dstName)
	if err != nil {
		// Handle this somehow...
	}
	defer dst.Close()

	written, err := io.Copy(dst, src)
	if err != nil {
		// Handle this somehow...
	}
}

For production software this is absolutely essential and despite being verbose it is great design. But it hurts for simple scripting tasks where we would prefer that it simply panic, as python/java/etc would.

So let's just tell it to handle it for us:

	func CopyFile(dstName, srcName string) {
		src, err := os.Open(srcName)
		handle.Err(err)
		defer src.Close()

		dst, err := os.Create(dstName)
		handle.Err(err)
		defer dst.Close()

		written, err := io.Copy(dst, src)
		handle.Err(err)
	}

That is the purpose of this simple package.

About

Handle go(golang) errors in scripts with a one-liner

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages