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: C, Get the Digits from a Number

Today’s Code of the Day is about how to find the digit of a number at a given position. I decided to go with the C / C++ programming languages to time, but you can easily translate the algorithms to other languages.

C Code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <math.h>
 
// Function: getDigitFromNum returns a digit at a given index of a integer.
// Goes from right to left with the starting index of 0.
int getDigitFromNum( int num, int digit ){
    num /= pow( 10, digit );
    return num % 10;
}
// Function: getDigitFromDec returns a digit at a given index of a double.
// Goes from left to right with the starting index of 0.
int getDigitFromDec( double dec, int digit ){
    dec *= pow( 10, digit );
    return (int)dec % 10;
}
// Function: getDigitFromNum returns the decimal values of a double.
double getDecimals( double dec ){
    return dec - (int)dec;
}

Example:
Input File

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Programmed by Larry Battle, bateru.com/news
// Date: April 26, 2011
// Purpose: To demonstrate how to get a digit by a position index.
#include <stdio.h>
#include <math.h>
 
int getDigitFromNum( int num, int digit ){
    num /= pow( 10, digit );
    return num % 10;
}
int getDigitFromDec( double dec, int digit ){
    dec *= pow( 10, digit );
    return ((int)dec) % 10;
}
double getDecimals( double dec ){
    return dec - (int)dec;
}
 
void main(){
    double dec = 1234.56789;
    printf( "\n%d", getDigitFromNum( (int)dec, 0 ));
    printf( "\n%d", getDigitFromDec( dec, 2 ));
    printf( "\n%f", getDecimals( dec ));
}

Output:

1
2
3
4
6
0.567890

Larry Battle

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

More Posts - Website

Follow Me:
TwitterLinkedInYouTube