Created
December 13, 2019 11:53
-
-
Save paulohrpinheiro/2a1b64319dde4f8c109017dbd95f8629 to your computer and use it in GitHub Desktop.
Working with unknown types in go
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" | |
// Void - a generic type | |
type Void struct { | |
value interface{} | |
} | |
func work(universe map[int]interface{}, v Void) { | |
value := v.value | |
switch t := value.(type) { | |
default: | |
fmt.Println("Unknow", t) | |
case int: | |
universe[1] = v | |
} | |
} | |
func main() { | |
myInt := 100 | |
data := Void{} | |
data.value = myInt | |
universe := make(map[int]interface{}) | |
work(universe, data) | |
fmt.Println(universe) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment