SoFunction
Updated on 2025-03-05

golang implements interface{} to other types of operations

The string in golang can be converted into a byte array or a rune array

But in fact, the type corresponding to byte is uint8, and the data type corresponding to rune is int32

So string can be converted to four types

//Interface to other types————The return value is interface, and direct assignment cannot be converted //Interface to string var a interface{}
 var str5 string
 a = "3432423"
 str5 = a.(string)
 (str5)
 
 //Interface to int var m interface{}
 var m1 int
 m = 43
 m1 = m.(int)
 (m1)
 
 //Interface to float64 var ff interface{}
 var ff1 float64
 ff = 432.54
 ff1 = ff.(float64)
 (ff1)

Supplement: golang interface to string,int,float64

Look at the code ~

func interface2String(inter interface{}) { 
  switch inter.(type) {
 
  case string:
    ("string", inter.(string))
    break
  case int:
    ("int", inter.(int))
    break
  case float64:
    ("float64", inter.(float64))
    break
  } 
}
 
func main() {
  interface2String("jack")
  interface2String(1)
  interface2String(12.223)
}
string jack
int 1
float64 12.223

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.