console.log for golang

If you are like me coming from a Nodejs background, then there is a possibility that the whole logging using fmt.Printf and log.Printf and fmt.Errorf  in golang may prove counter intuitive…So I made up the Nodejs/JS console.log like package that works on similar lines. You can get it by importing “github.com/julianfrank/console”

import (

"github.com/julianfrank/console"

)

then you can use it just like in javascript

console.Log("Life is cool but Chennai is hot")
var city:="Chennai"

var feel:="Hot"

console.Log("%s is %s !",city,feel) //Outputs Chennai is Hot !

For Errors it gets even better as it returns the string formatted as per your pattern and returns an error object

err:=myerroneousFunction()

errorObjectIWantToPassToLogger:=console.Error("Got this error in my function %s",err.Error()

If logging to console needs to be restricted then just trn the LogMode and ErrorMode on and off

console.ErrorMode=true//Turn on Logging to console on call

console.LogMode=false//Stops Logging to console

Hope this makes life simpler! Enjoy!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s