[Golang] Golang 的 Set

Set in Golang

R4 Cheng
May 3, 2023

前言

某天在使用 Golang 的時候,突然發現原來 Golang 沒有像其他語言一樣有實作 Set 的 data structure,稍微爬了文後發現,原來 Golang 會用 map 來模擬 Set ,以下是僅是小小的紀錄。

用 map 模擬 Set

map 是一個 key-value pair 的 data structure,keyvalue 可以是任意 type,並且 key 必須要是唯一。正因為 key 必須要是唯一的,所有重複的 key 將會被刪除,以達到實作 Set 的目的。

作法:將 string 作為 key 並將 value 設為 true

Eg:

mySet := map[string]bool{}
mySet["apple"] = true
mySet["banana"] = true

// 檢查某個值是否在 set 中
if mySet["orange"] {
fmt.Println("orange 在 set 中")
} else {
fmt.Println("orange 不在 set 中")
}

// iterate all values in set
for key, _ := range mySet {
fmt.Println(key)
}

// 獲取 set 的大小
fmt.Println(len(mySet))

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

R4 Cheng
R4 Cheng

Written by R4 Cheng

「0」が過去で「1」が未来「今」は何処にもない

No responses yet

Write a response