Skip to content

Instantly share code, notes, and snippets.

@osmanmakal
Created June 26, 2018 21:40
Show Gist options
  • Select an option

  • Save osmanmakal/8652c4854b103391dad6dac7525cda27 to your computer and use it in GitHub Desktop.

Select an option

Save osmanmakal/8652c4854b103391dad6dac7525cda27 to your computer and use it in GitHub Desktop.
How to determine an interface{} value type and existing values
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