Posts

Showing posts from February, 2022

Javascript: How to Sort a regular object based on key and value

Javascript: How to Sort a regular object based on key and value Assume like you have an object { '1': 1, '3': 1, '4': 3, '5': 1 } and you would like to sort it by its value. Then you can follow below way. I just added debug information to help with visulization but the function you need is simply Sort by value Object . entries ( obj ) . sort ( function ( a , b ) { return a [ 1 ] - b [ 1 ] } ) ; And if you would like to sort by key then Object . entries ( obj ) . sort ( function ( a , b ) { return a [ 0 ] - b [ 0 ] } ) ; Debug Code > Object . entries ( obj ) [ [ '1' , 1 ] , [ '3' , 1 ] , [ '4' , 3 ] , [ '5' , 1 ] ]

Notes: GoLang - Types & Methods - Part I

Notes: GoLang - Types & Methods - Part I Go is a statically typed language with both built in types and user defined types. Abstraction methods allowed in go, which allowing you to write code that invokes methods without inplementation. Types in Go Like we define structs, we can also define any other type in go, for example all below declarations are valid in go type Student struct { rollno int name string marks int } type AvgMarks float type Section string type Avg func ( int ) AvgMarks type ClassAvgMarks func ( int , int ) AvgMarks Types like these can be declared at level with no restrictions but type can be accessed within its scope unless they are expored package block level types. Abstract Types is one that specifies what a type should do, but not how it is done. Concrete Type specifies what and how, like implementation of the method. Methods On userdefined types like above, methods can be declared. If we define any me

Notes: GoLang - Maps vs Slices and Using Slice as buffer

Notes: GoLang - Maps vs Slices and Using Slice as buffer Maps vs Slices In go maps are refernce oriented. If you pass a map to a function as argument, any changes to that map inside the function will reflect to original function as well. package main import ( "fmt" ) func main ( ) { var testMap = make ( map [ string ] string ) testMap [ "lang" ] = "go" fmt . Println ( "Before updating the map" ) fmt . Println ( "-------------" ) for key , val := range testMap { fmt . Println ( key , val ) } _ = updateMap ( testMap ) fmt . Println ( ) fmt . Println ( "Before updating the map in another function" ) fmt . Println ( "-------------" ) for key , val := range testMap { fmt . Println ( key , val ) } } func updateMap ( testMap map [ string ] string ) map [ string ] string { testMap [ "type" ] = "compiled"

Notes: GoLang

Notes: GoLang GoLang Notes Outline Iterate Over String Switch Case Execute Shell Commands Iterate_Over_String var name string ; name = "GoLang" ; for index , value := range name { fmt . Printf ( "Index %d Char %c\n" , index , value ) ; } Switch-Case for _ , value := range "Golang" { switch value { case 'G' : fmt . Println ( "Case G executed" ) case 'l' : fmt . Println ( "Case I executed" ) } } Execute-Shell-Commands Ref: https://pkg.go.dev/os/exec@go1.17.6 package main import ( "fmt" "os/exec" ) func main ( ) { command := "uptime" output , error := exec . Command ( command ) . Output ( ) if error != nil { fmt . Println ( "Received error while executing the command" ) } else { fmt . Printf ( "Command %s output %s " , command , output ) } }

Few Tips To Overcome Unstable Mind

Few Tips To Overcome Unstable Mind The silly truth I realized, when you have fears, and if your mind becomes unstable, then events happening in your day might take over you and your day and that will lead to procrastination , the feeling of dont want to do anything. In some other words, we might also call it as depression and if you also feel that the task at your hand is either completely new to you or you dont like at all. But if you like the task at your hand then all these fears, events that might unstable you will not have any impact. So to make or build the task which you dont like you have to break the task into into known and unknown piece. Then devide your time. When your mind is stable, focus on unknown things because in tough times to move forward what we need is hope and will power which are like twin sisters, one doesn’t exist without the other. When your mind is not stable focus on known things which can be completed with no effort, focus on thi