This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package array | |
func maxSubArraySum(a []int, size int) int { | |
maxSoFar, currMax := a[0], a[0] | |
for i := 1; i < size; i { | |
currMax = max(a[i], currMax a[i]) | |
maxSoFar = max(maxSoFar, currMax) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package stack | |
// Stack represents a basic stack data structure. | |
type Stack[T any] struct { | |
items []T | |
} | |
// Push adds an item to the top of the stack. | |
func (s *Stack[T]) Push(item T) { | |
s.items = append(s.items, item) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package stack | |
// Stack represents a basic stack data structure. | |
type Stack struct { | |
items []any | |
} | |
// Push adds an item to the top of the stack. | |
func (s *Stack) Push(item any) { | |
s.items = append(s.items, item) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package queue | |
// Queue represents a generic queue data structure. | |
type Queue[T any] struct { | |
items []T | |
} | |
// Enqueue adds an item to the end of the queue. | |
func (q *Queue[T]) Enqueue(item T) { | |
q.items = append(q.items, item) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package queue | |
// Queue represents a basic queue data structure. | |
type Queue struct { | |
items []any | |
} | |
// Enqueue adds an item to the end of the queue. | |
func (q *Queue) Enqueue(item any) { | |
q.items = append(q.items, item) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// orderedmap implements an ordered map (ordered dictionary or linked hash map). | |
// This can be useful in scenarios where we need to maintain the order of access and/or insertion (ex: LRU Cache). | |
// For learning purposes. Use container/list. | |
package orderedmap | |
func NewOrderedMap() *OrderedMap { | |
return &OrderedMap{ | |
dict: make(map[string]*Node), | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package httprequest | |
import ( | |
"context" | |
"fmt" | |
"io" | |
"net/http" | |
) | |
// You will need a builder pattern |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
/* | |
In go, primitive types, and structs containing only primitive types, are copied by value. | |
You can copy them by simply assigning to a new variable (or returning from a function). | |
If your struct happens to include arrays, slices, or pointers, then you'll need to perform a deep copy of the referenced objects | |
unless you want to retain references between copies. | |
Source: https://stackoverflow.com/questions/51635766/how-do-i-copy-a-struct |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package storage | |
import ( | |
"errors" | |
"io" | |
) | |
// Product interface | |
type Storage interface { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package designpattern | |
import ( | |
"sync" | |
) | |
type singleton struct {} | |
var instance *singleton | |
var once sync.Once |
NewerOlder