Recent Posts

Go - mutual exclusion

Ex. Binary semaphore Race Condition Semaphore Ex. sync.Mutex Mutex.Unlock happens before Mutex.Lock Every Lock needs Unlock, can use defe...

Go - prevent race condition

Prevent race condition Initialize variables when package initialization phase, it will happen before main function. And don't modify those variables a...

Go - Cancel Job by close channel

Close a channel to broadcast cancellation No way to know a channel is closed or not, so need another channel to indicate a channel is closed This exa...

Go - Select Channel

Declare select {     case <- ch:         ...     case x := <- ch2:       &...

Go - parallel iteration

Inner goroutine can send event to outer goroutine by channel Ex. Pass value to inner routine function Declare a variable in inner goroutine function, ...

Go - buffered channel

Declare a queue: ch := make(chan int, 3) Block send when full, block receive when empty Get capacity: cap(ch) Get currently buffered: len(ch) ...

Go - unidirectional channel

Send only: chan<- int Receive only: <-chan int Violation will compile error Only channel used to send msg need to be closed, so to cl...

Go - pipelines

Ex. Make 3 channels and chan together, print in fmt.Println(<-channel) There is no way to check is a channel closed Close pipeline safely. Check rece...

Go - unbuffered channel

Unbuffered channel causes sender and receiver synchronized, called synchronized channel "When a value is sent on an unbuffered channel, the receipt of ...

Go - channel - basic

Declare ch := make(chan int) // unbuffered, send will be blocked until receive was called ch := make(chan int, 0) // unbuffered, send will be b...

Go - goroutine

Run f() in another goroutine: go f() To produce a race condition issue, can make for loop larger

Go - Type Switch

switch x.(type) { case: 。。 default:..} No fallthrough is allowed Reuse assert type result. switch x := x.(type) {...} Can combine more than one t...

Go - Type Assertion

X.(T)  X is interface, T is concrete type. When T is a concrete type, it assert dynamic type of X is identical to T. Result is the dynamic value wi...

Go - Interface value

Real type of an interface variable is decided dynamatically Need be careful when comparing interface, panic will occur when real type is not comparable ...

Go - Interface Satisfaction - 2

When a type value assign to a variable, it can call method declared with type value and pointer When a type pointer assign to a variable, it can call...

Go - Interface Satisfaction - 1

A function declared with type (not a pointer), can call method by type value. Can use (&t) to call method by address as well When a function d...

Go - Interface Contracts

Don't need implement interface, only method is enough Compile error if signature is wrong,  Although don't need declare "implements", but sti...

Go - encapsulation

Access control: upper case ecported to diiferent package It means the unit of encapsulation is package Difference Ex. type A int, can’t be encapsul...