こんにちは。KOUKIです。
とあるWeb系企業でGoエンジニアをしています。
Go言語には、Map型があります。
1 2 |
# 例 key int, value string var myMap map[int]string |
そして、このMapの処理(get, pop,push,delete)を自作してみました!
特に意味がある行動ではありませんが(w)、頭の体操になると思いますのでぜひ一読ください。

<目次>
Map構造体の定義
Mapの処理(get, pop,push,delete)は、構造体を定義してそのメソッドとして実装したいと思います。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package main import "fmt" type myArray struct { length int data map[int]string } func NewMyArray() myArray { return myArray{ length: 0, data: make(map[int]string), } } |
Get
Getは、Mapからkeyに一致するデータを返します。
1 2 3 |
func (m *myArray) get(index int) string { return m.data[index] } |
Push
Pushは、Mapにデータを投入します。
1 2 3 4 5 |
func (m *myArray) push(item string) int { m.data[m.length] = item m.length += 1 return m.length } |
Pop
Popは、Mapに格納されている最後のデータを削除します。
1 2 3 4 5 6 |
func (m *myArray) pop() string { lastItem := m.data[m.length-1] delete(m.data, m.length-1) m.length-- return lastItem } |
Delete
Deleteは、Mapからキーに一致するデータを削除します。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
func (m *myArray) delete(index int) string { item := m.data[index] m.shiftItems(index) return item } func (m *myArray) shiftItems(index int) { for i := index; i < m.length-1; i++ { m.data[i] = m.data[i+1] } delete(m.data, m.length-1) m.length-- } |
検証
検証してみましょう。
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 29 30 |
func main() { newArray := NewMyArray() fmt.Println("----------push---------") fmt.Println(newArray.push("hi")) fmt.Println(newArray.push("you")) fmt.Println(newArray.push("!")) fmt.Println(newArray.push("?")) fmt.Println("----------get---------") fmt.Println(newArray.get(0)) fmt.Println(newArray.get(1)) fmt.Println(newArray.get(2)) fmt.Println(newArray.get(3)) fmt.Println("----------pop---------") fmt.Println(newArray.pop()) fmt.Println(newArray.pop()) fmt.Println("---------delete--------") fmt.Println(newArray.delete(0)) fmt.Println("----------Done---------") newArray.push("are") newArray.push("nice") newArray.delete(1) fmt.Println(newArray) } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$ go run main.go ----------push--------- 1 2 3 4 ----------get--------- hi you ! ? ----------pop--------- ? ! ---------delete-------- hi ----------Done--------- {2 map[0:you 1:nice]} |
まとめ
前述の通り特に意味があるコードではないですが、他のプログラミング言語でもよく見かける処理だと思います。
データのアルゴリズムを考える上でも頭の体操になると思いますので、ぜひチャレンジしてください。
それでは、また!
Go記事まとめ
ソースコード
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
package main import "fmt" type myArray struct { length int data map[int]string } func NewMyArray() myArray { return myArray{ length: 0, data: make(map[int]string), } } func (m *myArray) get(index int) string { return m.data[index] } func (m *myArray) push(item string) int { m.data[m.length] = item m.length += 1 return m.length } func (m *myArray) pop() string { lastItem := m.data[m.length-1] delete(m.data, m.length-1) m.length-- return lastItem } func (m *myArray) delete(index int) string { item := m.data[index] m.shiftItems(index) return item } func (m *myArray) shiftItems(index int) { for i := index; i < m.length-1; i++ { m.data[i] = m.data[i+1] } delete(m.data, m.length-1) m.length-- } func main() { newArray := NewMyArray() fmt.Println("----------push---------") fmt.Println(newArray.push("hi")) fmt.Println(newArray.push("you")) fmt.Println(newArray.push("!")) fmt.Println(newArray.push("?")) fmt.Println("----------get---------") fmt.Println(newArray.get(0)) fmt.Println(newArray.get(1)) fmt.Println(newArray.get(2)) fmt.Println(newArray.get(3)) fmt.Println("----------pop---------") fmt.Println(newArray.pop()) fmt.Println(newArray.pop()) fmt.Println("---------delete--------") fmt.Println(newArray.delete(0)) fmt.Println("----------Done---------") newArray.push("are") newArray.push("nice") newArray.delete(1) fmt.Println(newArray) } |
コメントを残す
コメントを投稿するにはログインしてください。