Last active
June 7, 2021 10:15
-
-
Save vaguecoder/3a93f51f5b98ee4537e864fcb49593d7 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"fmt" | |
"net/http" | |
"os" | |
) | |
// HashMap is the implementation of a hash map in Go | |
type HashMap map[interface{}]interface{} | |
// Person is a random struct type for demonstration purposes | |
type Person struct { | |
Name string | |
Age int | |
Phone string | |
} | |
// Print prints the key-value mappings alone with the data types of a HashMap | |
func (h *HashMap) Print() { | |
for k, v := range *h { | |
fmt.Printf(">>> %v (%T) ----> %v (%T)\n\n", k, k, v, v) | |
} | |
} | |
func main() { | |
h := make(HashMap) | |
// Mapping String -> *http.Request | |
h["HTTP-Request"] = &http.Request{} | |
// Mapping os.File -> *os.File | |
h[os.File{}], _ = os.Open("blankfile") | |
// Mapping struct{} -> int | |
h[struct{}{}] = 6 | |
// Mapping bool -> map[string]string | |
h[true] = make(map[string]string) | |
// Mapping custom defined Person struct-> custom defined HashMap type | |
h[Person{}] = HashMap{} | |
h.Print() | |
} | |
/* | |
Output: | |
>>> HTTP-Request (string) ----> &{ <nil> 0 0 map[] <nil> <nil> 0 [] false map[] map[] <nil> map[] <nil> <nil> <nil> <nil>} (*http.Request) | |
>>> {<nil>} (os.File) ----> &{0xc00005e2a0} (*os.File) | |
>>> {} (struct {}) ----> 6 (int) | |
>>> true (bool) ----> map[] (map[string]string) | |
>>> { 0 } (main.Person) ----> map[] (main.HashMap) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment