Skip to content

Commit

Permalink
feat: add map trap
Browse files Browse the repository at this point in the history
  • Loading branch information
Leizhenpeng committed Feb 11, 2023
1 parent 64aa2c7 commit 7831962
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 31 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.idea
.idea

00-playground
44 changes: 22 additions & 22 deletions 05-options-pattern/main.go
Original file line number Diff line number Diff line change
@@ -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
12 changes: 6 additions & 6 deletions 05-options-pattern/options_way.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
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)
}
return b
}

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
}
}
Expand Down
12 changes: 11 additions & 1 deletion 06-range-expression/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 27 additions & 0 deletions 07-map-trap/main.go
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions 07-map-trap/map_struct.go.fake
Original file line number Diff line number Diff line change
@@ -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")
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Sharing is free but caring is priceless. [So, now please click here](https://www

_好的习惯会给无聊代码带来一阵清风._

* **[range后面的表达式]()**
* **[你关注过range后面的表达式吗]()**

_不同于for,range后接的表达式只会被求值一次._

Expand Down

0 comments on commit 7831962

Please sign in to comment.