Module not found Error - Can't resolve 'crypto'

To fix Module not found: Error: Can't resolve 'crypto' and Module not found: Error: Can't resolve 'stream' error, you can try this :

npm install 'stream-browserify'
npm install 'crypto-browserify'

Anchor test - Error loading workspace IDL for counter

When encountering this error Error: Error loading workspace IDL for counter when run anchor test, you shoud check three place which must be same name.

  1. program ‘s name
     pub mod programe_name
    
  2. package name in Cargo.toml
     [package]
     name = "programe_name"
     version = "0.1.0"
     description = "Created with Anchor"
     edition = "2021"
    
  3. lib name in Cargo.toml
     [lib]
     crate-type = ["cdylib", "lib"]
     name = "counter"
    

Remote Procedure Call

什麼是 RPC (remote procedure call) ?

跨越了 OSI 模型中的傳輸層與應用層的遠程調用方式,在 client-server 的概念下,使雙方可以向調用本地服務一樣調用遠程服務。

簡單整理幾種常見的RPC

SOAP

  • 全名: Simple Object Access Protocol
  • 雖然名字有 sample,但其實有點麻煩,需要建立 XML 檔案來與其他電腦溝通或呼叫
  • 依賴於標準協議如 HTTP、SMTP,但這些協議所有作業系統都支援

REST

  • 全名:Representational State Transfer
  • 和 SOAP 一樣,依賴標準協議 HTTP 來和其他服務溝通,然而在資料格式上比較彈性,可以使用JSON

JSON-RPC

  • 使用 JSON 格式
  • 格式相對 REST 與 SOAP 自由

GRPC

  • 和 REST 與 SOAP 相同,使用 HTTP 作為傳輸層的協議
  • 不像 REST 有定義好的 GET 和 PUT 等,開發者可以定義任何類型的 function call
  • 調用遠程的服務對 client 和 server 來說都像是在本地調用,避免了遠程調用產生的編碼複雜性。這個能力使 GRPC 在需要大量遠程調用的環境很流行
  • 跑得快

GraphQL

  • 做得像 Query Language 一樣
  • 可以清楚定義想取得的資料,精準拿取想要的資料

Thrift

  • 可以跨語言使用

好文整理

既然有HTTP协议,为什么还要有RPC

Go - map

Go 中的 Map 是無序的 key-value 集合,訂好型別後,key 必須是同一種型別,value 也必須都是同一種型別。 以下是一些常使用到的語法

m := make(map[string]int) // 建立 key 是 string 、 value 是 int 的 map
m["ten"] = 10 // 將 key "ten" 的 value 設為 10
m["two"] = 2

length := len(m) // 取得目前有多少 key
fmt.Println("map: ", m)
fmt.Println("how many keys ?", length)

delete(m, "ten") // 刪除 ten 這個 key
fmt.Println("map: ", m)

_, isExist := m["ten"] // 取得 "ten" 這個 key 是否存在於 map 中
_, isExist2 := m["two"]
fmt.Println("Does ten exist ?", isExist)
fmt.Println("Does two exist ?", isExist2)

// output :
// map:  map[ten:10 two:2]
// how many keys ? 2
// map:  map[two:2]
// Does ten exist ? false
// Does two exist ? true

Prefix Trie

在寫 Leetcode 的時候認識了這個資料結構:Prefix Trie 前綴樹。
Prefix Trie 在 Insert、Search、Delete 皆只需要 O(n) 的時間。

圖解 Insert 過程

相關 Leetcode 題目