Maps and memory leaks(#28)

前言 此為跟<learn go with tests>讀書會成員在分享Maps章節,有人分享的文章,原文見於100 Go Mistakes and How to Avoid Them,此篇針對作者提到的第28個常見錯誤,做一些讀後記錄。 Map in Go 此篇不會介紹在go裡面如何操作map, 而是希望更了解map設計原理,並藉由文章來探討為什麼可能發生memory leaks. 在Go的官方部落格關於map是以此做為開頭 One of the most useful data structures in computer science is the hash table. Many hash table implementations exist with varying properties, but in general they offer fast lookups, adds, and deletes. Go provides a built-in map type that implements a hash table. 在計算機科學的世界裡面,hash table是一種很常使用的資料結構。hash table會用hash function將key產出獨特的hash value,然後分配到不同的bucket。在Python裡面有dictionary, 在Go裡面則是透過Map實現hash table Map可看成由一個陣列構成,而陣列的每個元素都是指向一個bucket的pointer。每個bucket包含了一系列的key-value pairs。當要尋找或是新增Map中的元素時,會使用key的hash valuse來確定其在陣列中的位置,然後在對應的桶中進行操作。...

January 18, 2024 · 3 min