From 7831962a0cff406e89ecd24b622ad00840826e13 Mon Sep 17 00:00:00 2001 From: river Date: Sat, 11 Feb 2023 16:23:43 +0800 Subject: [PATCH] feat: add map trap --- .gitignore | 4 ++- 05-options-pattern/main.go | 44 +++++++++++++++---------------- 05-options-pattern/options_way.go | 12 ++++----- 06-range-expression/main.go | 12 ++++++++- 07-map-trap/main.go | 27 +++++++++++++++++++ 07-map-trap/map_struct.go.fake | 35 ++++++++++++++++++++++++ readme.md | 2 +- 7 files changed, 105 insertions(+), 31 deletions(-) create mode 100644 07-map-trap/main.go create mode 100644 07-map-trap/map_struct.go.fake diff --git a/.gitignore b/.gitignore index 723ef36..e325e91 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -.idea \ No newline at end of file +.idea + +00-playground \ No newline at end of file diff --git a/05-options-pattern/main.go b/05-options-pattern/main.go index 84158e1..4cc82df 100644 --- a/05-options-pattern/main.go +++ b/05-options-pattern/main.go @@ -1,27 +1,27 @@ -// package main +package main -// type Book struct { -// Title string -// ISBN string -// } +type Book struct { + Title string + ISBN string +} -// func NewBook(t string) *Book { -// return &Book{ -// Title: t, -// ISBN: "", -// } -// } +func NewBook(t string) *Book { + return &Book{ + Title: t, + ISBN: "", + } +} -// func NewBook2(t string, i string) *Book { -// return &Book{ -// Title: t, -// ISBN: i, -// } -// } +func NewBook2(t string, i string) *Book { + return &Book{ + Title: t, + ISBN: i, + } +} -// func main() { -// _ = NewBook("早点下班的go语言学习法") -// _ = NewBook2("早点下班的go语言学习法", "20230101") -// } +func main() { + _ = NewBook("早点下班的go语言学习法") + _ = NewBook2("早点下班的go语言学习法", "20230101") +} -// // How To Make Constructor Better +// How To Make Constructor Better diff --git a/05-options-pattern/options_way.go b/05-options-pattern/options_way.go index 31d9293..616a10e 100644 --- a/05-options-pattern/options_way.go +++ b/05-options-pattern/options_way.go @@ -1,13 +1,13 @@ package main -type Book struct { +type Book___ struct { Title string Code string } -type Option func(*Book) +type Option func(*Book___) -func NewBook_(options ...Option) *Book { - b := &Book{} +func NewBook_(options ...Option) *Book___ { + b := &Book___{} for _, option := range options { option(b) } @@ -15,13 +15,13 @@ func NewBook_(options ...Option) *Book { } func WithTitle(title string) Option { - return func(b *Book) { + return func(b *Book___) { b.Title = title } } func WithISBN(ISBN string) Option { - return func(b *Book) { + return func(b *Book___) { b.Code = ISBN } } diff --git a/06-range-expression/main.go b/06-range-expression/main.go index 2147475..0fff06d 100644 --- a/06-range-expression/main.go +++ b/06-range-expression/main.go @@ -4,8 +4,18 @@ import "fmt" func main() { s := []string{"something"} - for range s { + for i := 0; i < len(s); i++ { s = append(s, "new_one") + fmt.Println(s) } } + +// Which Output is correct: +// A. [something new_one] + +// B. [something new_one], +// [something new_one new_one ] +// [something new_one new_one new_one] ... + +// C. panic: runtime error: index out of range diff --git a/07-map-trap/main.go b/07-map-trap/main.go new file mode 100644 index 0000000..64170de --- /dev/null +++ b/07-map-trap/main.go @@ -0,0 +1,27 @@ +package main + +import "fmt" + +func main() { + m := map[string]int{ + "rust": 1, + "go": 2, + "cpp": 3, + } + //copy one + m_copy := make(map[string]int, len(m)) + for k, v := range m { + m_copy[k] = v + } + + for k, v := range m_copy { + m[k+"_new"] = v + 100 + } + + fmt.Printf("map length: %d\n", len(m)) +} + +// Output +// A: 3 +// B: 6 +// C: I don't know diff --git a/07-map-trap/map_struct.go.fake b/07-map-trap/map_struct.go.fake new file mode 100644 index 0000000..1a5c97c --- /dev/null +++ b/07-map-trap/map_struct.go.fake @@ -0,0 +1,35 @@ +//fake code + +package main + +type Map struct { + size int + buckets []Bucket +} +type Bucket struct { + key Key + value Value +} + +func (m *Map) Set(key Key, value Value) { + // Compute the hash of the key. + hash := hashFunction(key) + // Determine the index of the bucket for this key. + index := hash % m.size + m.buckets[index] = Bucket{key: key, value: value} +} + +func (m *Map) Get(key Key) (Value, bool) { + hash := hashFunction(key) + index := hash % m.size + // Retrieve the value for the key from the bucket. + if m.buckets[index].key == key { + return m.buckets[index].value, true + } else { + return Value{}, false + } +} + +func hashFunction(key invalid type) { + panic("unimplemented") +} diff --git a/readme.md b/readme.md index 872ead4..50f23a4 100644 --- a/readme.md +++ b/readme.md @@ -19,7 +19,7 @@ Sharing is free but caring is priceless. [So, now please click here](https://www _好的习惯会给无聊代码带来一阵清风._ -* **[range后面的表达式]()** +* **[你关注过range后面的表达式吗]()** _不同于for,range后接的表达式只会被求值一次._