Google Go: recover() example

Here’s an example of how to use recover() in Google Go.

package main

import "fmt"

func gandalf( doTalkTo bool ){
	defer catch()
	if(doTalkTo){
		// Panic stops execution of the current function and stops unwinding the stack, calling an deferred functions along the way.
		panic("You shall not pass!")	
	}
}
// catch() must be called as a deferred 
func catch(){
	// recover() regains control over the program execution when panic() is called.
	// recover() returns is the argument passed from panic()
	if r := recover(); r != nil {
		fmt.Println("Error:", r)
	} else {
		fmt.Println("No problems occurred")
	}
}
func main() {
	gandalf(true)
	gandalf(false)
	//unwinds to the top of the stack and ends the program
	panic("Where am I going?");
}


Output:
Error: You shall not pass!
No problems occurred
panic: Where am I going?

goroutine 1 [running]:
main.main()

Demo: http://play.golang.org/p/xu9Jd1YI0T

More information here:
Effective Go - The Program Language

Larry Battle

I love to program, and discover new tech. Check out my stackoverflow and github accounts.

More Posts - Website

Follow Me:
TwitterLinkedInYouTube

Code of the day: Javascript check if an object is empty

`isObjectEmpty()` is a function that will only return true if the passed an object that contains no keys.
This is different than jQuery.isEmptyObject() because the passed argument MUST be an object.

/**
* Check to see if an object has any keys.
* @param {Object} obj
* @return {Boolean}
* @author Larry Battle <bateru.com/news>
* @license WTFPL
**/
var isObjectEmpty = function( obj ) {
	var name;
	for ( name in obj ) {
		return false;
	}
	return obj != null && typeof obj === "object";
};

Larry Battle

I love to program, and discover new tech. Check out my stackoverflow and github accounts.

More Posts - Website

Follow Me:
TwitterLinkedInYouTube

OpenCL Demo

OpenCL is a standards-based programming language that allows the CPU and GPU to work together for faster and more efficient processing. OpenCL gives any application access to the graphics processing unit for non-graphical computing. Thus, OpenCL extends the power of the Graphics Processing Unit beyond graphics.

Check out this demo comparing OpenCL to C and a multi-threaded program.

Larry Battle

I love to program, and discover new tech. Check out my stackoverflow and github accounts.

More Posts - Website

Follow Me:
TwitterLinkedInYouTube

Gallery Lister 0.6 Beta release

Hey Everyone,
Here’s a old script that I made back in 2007 called gallery Lister.
Basically it’s a simple link generator that generates links in numerical order. It’s really useful when viewing images all on one page.
You can check it out here, Gallery Lister Beta.

Summary:
    This is a javascript batch search script. It generates links or images of sequence data. -: Source
Usage:
    (*start number - *end number).jpg
Example:
    http://www.example.com/pics/(01-30).jpg
Result:
    http://www.example.com/pics/01.jpg
    http://www.example.com/pics/02.jpg
    ...
    http://www.example.com/pics/29.jpg
    http://www.example.com/pics/30.jpg
Tip:
    You can search multiple directories at once with the following format
    http://www.asdf.com/data/(234-235)/(34-54).jpg 

Larry Battle

I love to program, and discover new tech. Check out my stackoverflow and github accounts.

More Posts - Website

Follow Me:
TwitterLinkedInYouTube