Created
June 26, 2018 21:40
-
-
Save osmanmakal/8652c4854b103391dad6dac7525cda27 to your computer and use it in GitHub Desktop.
How to determine an interface{} value type and existing values
This file contains hidden or 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" | |
| "reflect" | |
| ) | |
| // How to determine an interface{} value type and existing values... | |
| func main() { | |
| value := make(map[string]interface{}) | |
| value["dogru"] = string("hebele, hübele") | |
| // interface conversion anında patlamamak içindir. | |
| if _, ok := value["dogru"]; ok { | |
| fmt.Println("Key *dogru* interface var!") | |
| } | |
| // Normalde olmayan key için yazılım patlar! | |
| // bu şekilde keyi valide edebilirsimiz. | |
| if _, ok := value["yanlis"]; !ok { | |
| fmt.Println("Key *yanlis* interface yok!") | |
| } | |
| // Aynı zamanda key valuesinide alabilirsiniz. | |
| if val, ok := value["dogru"]; ok { | |
| fmt.Println("Key *dogru* için value: " + val.(string)) | |
| } | |
| // Inteface tipini de bu şekilde alabilirsiniz. | |
| if val, ok := value["dogru"]; ok { | |
| t := reflect.TypeOf(val) | |
| fmt.Println("Key *dogru* icin tip: " + t.String()) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment