Programming Notes ✍️ – Telegram
Programming Notes ✍️
12 subscribers
65 photos
1 file
22 links
Download Telegram
Note: Only the sender should close a channel, never the receiver. Sending on a closed channel will cause a panic.

Another note: Channels aren't like files; you don't usually need to close them. Closing is only necessary when the receiver must be told there are no more values coming, such as to terminate a range loop.
Atomicity: All operations in a transaction either succeed or all are rolled back.

Consistent: The database integrity constraints are valid on completion of the transaction.

Isolated: Simultaneously occurring transactions do not interfere with each other. Contentious concurrent access is moderated by the
database so that transactions appear to run sequentially.

Durable: Irrespective of hardware or software failures, the updates made by the transaction are permanent.
👍2
🔥1
🔥1
Clear dependencies
Clear syntax
Clear semantics
Composition over inheritance
Simplicity provided by the programming model (garbage collection, concurrency)
Easy tooling (the go tool, gofmt, godoc, gofix)
🔥1
Binary search using type casting an interface to []int

var numbers = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28}

func binarySearch(in interface{}, target int) int {
var num = in.([]int)
left, right := 0, len(num)-1
for left <= right {
mid := left + (right-left)/2
guess := num[mid]
if target == guess {
return mid
}
if target > guess {
left = mid + 1 // the guess was too high
}
if target < guess {
right = mid - 1 // the guess was too low
}
}
return -1
}

func main() {
fmt.Println(binarySearch(numbers, 20))
}
🔥1
running simple search & binary search in diffrenet gorutines and signaling using channels :

var TIMEOUT = 2 * time.Second

var numbers = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28}

func simpleSearch(input []int, target int) int {
for i := 0; i < len(input); i++ {
if input[i] == target {
return i
}
}
return 0
}

func binarySearch(in interface{}, target int) int {
var num = in.([]int)
left, right := 0, len(num)-1
for left <= right {
mid := left + (right-left)/2
guess := num[mid]
if target == guess {
return mid
}
if target > guess {
left = mid + 1 // the guess was too high
}
if target < guess {
right = mid - 1 // the guess was too low
}
}
return -1
}

func run(signal chan bool) {
var fun1 chan bool
fun1 = make(chan bool)

go func() {
timeBinary := time.Now()
fmt.Println(binarySearch(numbers, 20))
fmt.Println(time.Since(timeBinary))
fun1 <- true

}()

var fun2 chan bool
fun2 = make(chan bool)
go func() {
timeSimple := time.Now()
fmt.Println(simpleSearch(numbers, 20))
fmt.Println(time.Since(timeSimple))
time.Sleep(10 * time.Second)
fun2 <- true
}()

if <-fun1 && <-fun2 {
signal <- true
}
}

func main() {
sig := make(chan bool)
go run(sig)

fmt.Println("Welcome to the playground!")

select {
case <-sig:
fmt.Println("Runner Finished!!!")
case <-time.After(TIMEOUT):
fmt.Println("Timed out")
}
}


this proccess waits until the sig chan recive true or the time out get triggerd.
🔥1
🔥2
selection sort implementation


func selection(arr []int) []int {
length := len(arr)

for i := 0; i < length; i++ {
for j := i + 1; j < length; j++ {
if arr[i] > arr[j] {
arr[i], arr[j] = arr[j], arr[i]
}
}
}
return arr
}

func main() {
fmt.Println("Welcome to the Playground!")
numbers := nums
numbers = selection(numbers)
fmt.Println(numbers)
}
🔥1
وقتی بالا اوردن یه پروداکت انقدر پیچیدگی داره که حتی توی کتابم ازش صحبتی نمیشه :