↓ Skip to main content
Go

GoLang: try-catch

Table of Contents

GoLang doesn’t have a try and catch like the popular C++ or Java languages, what it has is panic-recover.

Non critical errors are returned as normal values and for the critical ones, panic is used

func myFunc(myvalue string) (string, error)

Creating Error
#

  • errors.New()
  • fmt.Errorf()
func throwError() (string, error) {
	return "", fmt.Errorf("I am an simple error")
}

func main() {
	_, err := throwError()

	if err != nil {
		fmt.Println(" catch error...." + err.Error())
	}
}
catch error....I am an simple error

Recover
#

Recover is a built-in function that allows the regain of control from a panicking goroutine. For the normal flow of execution, a call to recover will return nil and will have no effect.

For a panicking gomodule, call to recover will capture the value given to panic and resume normal execution.

Recover is only useful inside deferred functions.
func throwPanic() {
	fmt.Println("I will throw panic after few seconds.")
	defer func() {
		if r := recover(); r != nil {
			fmt.Printf(" Caught Panic: %v\n", r)
		}
	}()
	panic(fmt.Errorf("%s", "Panic has struck...."))
}

func main() {
	fmt.Println("main function")
	throwPanic()
	fmt.Println("I will now print...")
}
main function
I will throw panic after few seconds.
 Caught Panic: Panic has struck....
I will now print...

You can now see that in function throwPanic() we have defined a deferred function invocation in which the recover function will be invoked to see what error has happened and finally the normal flow resumes and hence the “I will now print” is returned.

Help
#

Author
Samarthya
Engineer, learner, tinkerer. This is my trophy shelf of everyday work and leisure.

Related

GoLang: encoding

Package encoding provide capabilities to convert data to and from byte-level and textual representations. encoding/gob encoding/json, and encoding/xml The gob is Go’s format for serializing and deserializing. The gob was tailored for communicating between two servers written in Go. Only exported fields are encoded; zero values are not taken into account Package gob manages streams of gobs – binary values exchanged between an Encoder (transmitter) and a Decoder (receiver). Format GoB # Let’s create a simple Type Blog and we will encode and decode it using the gob format.

GoLang: Hash

·2 mins
Previously I talked about files, encoding in golang. In this LTD (learn to do) I will talk about hash package in general. https://blog.samarthya.me/wps/2021/01/30/golang-encoding/ Help # https://golang.org/pkg/hash/#Hash https://golang.org/pkg/crypto/#Hash.New https://golang.org/src/crypto/crypto.go?s=364:378#L6 https://golang.org/pkg/crypto/#pkg-subdirectories Package crypto collects common cryptographic constants. — Official golang documentation const ( MD4 Hash = 1 + iota // import golang.org/x/crypto/md4 MD5 // import crypto/md5 SHA1 // import crypto/sha1 SHA224 // import crypto/sha256 SHA256 // import crypto/sha256 SHA384 // import crypto/sha512 SHA512 // import crypto/sha512 MD5SHA1 // no implementation; MD5+SHA1 used for TLS RSA RIPEMD160 // import golang.org/x/crypto/ripemd160 SHA3_224 // import golang.org/x/crypto/sha3 SHA3_256 // import golang.org/x/crypto/sha3 SHA3_384 // import golang.org/x/crypto/sha3 SHA3_512 // import golang.org/x/crypto/sha3 SHA512_224 // import crypto/sha512 SHA512_256 // import crypto/sha512 BLAKE2s_256 // import golang.org/x/crypto/blake2s BLAKE2b_256 // import golang.org/x/crypto/blake2b BLAKE2b_384 // import golang.org/x/crypto/blake2b BLAKE2b_512 // import golang.org/x/crypto/blake2b ) Let’s try out a simple MD5 hasher.

GoLang: Hex Output

·3 mins
In last few blogs I have written about reading commandline parameters reading files and using the os package. In this blog, I will combine some these items to read a file byte by byte using the bufio package func’s.