-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
64aa2c7
commit 7831962
Showing
7 changed files
with
105 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
.idea | ||
.idea | ||
|
||
00-playground |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters