Type casting in Golang;

Type casting for Primitives

func TypeCast() {  var intType int = 32  var float32Type float32 = float32(intType)  fmt.Println(float32Type)}

Type casting for Objects

type Sample struct {  name string}func TypeCast() {  var myMap = make(map[string]interface{})  myMap["Key1"] = "Value1"  myMap["Key2"] = &Sample{   name: "Value2",  }
for k, _ := range myMap { if typeVal, ok := findType(myMap[k]); ok { fmt.Println(typeVal) } }}func findType(val interface{}) (typeVal string, ok bool) { switch val.(type) { case string: return "String", true case *Sample: return "Sample", true } return "", false}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store