Skip to content

Instantly share code, notes, and snippets.

View vaguecoder's full-sized avatar
🤖
Confused

Bhargav Ravuri vaguecoder

🤖
Confused
View GitHub Profile
@vaguecoder
vaguecoder / Multilevel-JSON-Unmarshalling.go
Last active May 21, 2021 19:09
This takes JSON as object and returns slice of all the leaf key-value pairs in the structure. This doesn't preserve details of the depth, but just KVs of all levels. More details in: https://github.com/VagueCoder/Random-Go-Snippets/
package unmarshal
import (
"fmt"
"reflect"
)
// keyValuePairs takes in map[string]interface{} as interface{} and returns map[string]string
// In simpler words, it takes map of uncertain levels and returns the leaf key-value pairs.
func keyValuePairs(m interface{}) map[string]string {
@vaguecoder
vaguecoder / formattedTimeMarshalling.go
Last active May 21, 2021 19:10
Overrides the marshaller function to marshal time.Time object to a specific formatted time string, similar to: "21-May-2021 23:20:08 IST". More details in: https://github.com/VagueCoder/Random-Go-Snippets/
package formattedTime
import (
"fmt"
"regexp"
"time"
)
type FormattedTime string
@vaguecoder
vaguecoder / filesFilteredByTimeWindow.go
Last active May 21, 2021 19:10
This filters out the list of files/directories in the given window. More details in: https://github.com/VagueCoder/Random-Go-Snippets/
package main
import (
"fmt"
"io/ioutil"
"time"
)
func main() {
// Not handling any errors since just demonstration
@vaguecoder
vaguecoder / fastFibonacci.go
Last active May 21, 2021 19:09
Basic fibonacci code snippet with works in goroutines which doesn't give whole series, but n'th number. More details in: https://github.com/VagueCoder/Random-Go-Snippets/
package fibonacci
import "sync"
func Fibonacci(n int) int {
var wg sync.WaitGroup
var mu sync.Mutex
var a, b int = 0, 1
wg.Add(n)
@vaguecoder
vaguecoder / randomStrings.go
Created May 22, 2021 13:32
This creates a channel which continuously processes and returns random strings of fixed (given) size. And processing can be cancelled at will.
package randomStrings
import (
"context"
"math/rand"
"time"
)
// randInt is local function that returns a random integer between provided min, max.
func randInt(min int, max int) int {
package main
import (
"fmt"
"net/http"
"os"
)
// HashMap is the implementation of a hash map in Go
type HashMap map[interface{}]interface{}
@vaguecoder
vaguecoder / broadcast.go
Created July 4, 2021 23:11
Broadcasts a message (any type) continuously in another goroutine. For full package/program, check `GitHub.com/VagueCoder/Random-Go-Snippets`.
// Channel is a custom type to hold channel and context variables.
type Channel struct {
// Only unexported fields
ch chan interface{}
ctx context.Context
cancelFunc context.CancelFunc
}
// runProducer is creates a separate goroutine that continuously produces given message.
@vaguecoder
vaguecoder / FindGCD.go
Created August 23, 2021 10:11
Code to find the Greatest Common Divisor (GCD) of 2 or more numbers.
package main
import "fmt"
// PrimeGenerator creates a generator goroutine that returns prime numbers, least first
func PrimeGenerator(close <-chan int) chan int {
var current int
var flag bool
ch := make(chan int)
@vaguecoder
vaguecoder / Sample-Data-For-JSON-Operations.go
Last active December 16, 2021 18:00
JSON Marshal-Unmarshal vs Encoding-Decoding in Go (golang)
type Character struct {
Name string `json:"name"`
Designation []string `json:"designation"`
Age int `json:"age"`
Moves []Move `json:"moves"`
}
type Move struct {
Name string `json:"move"`
Power int `json:"power"`
var jsonData []byte
jsonData, err = json.Marshal(data)
if err != nil {
// Handle Error
}
fmt.Println(jsonData) // JSON data in bytes
fmt.Println(string(jsonData)) // String for format verification