Skip to content

Instantly share code, notes, and snippets.

@jeffotoni
Created June 19, 2021 13:26
Show Gist options
  • Save jeffotoni/2ef35964cb2339459571ff17ed26fbfc to your computer and use it in GitHub Desktop.
Save jeffotoni/2ef35964cb2339459571ff17ed26fbfc to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
func main() {
var i interface{}
i = "Go Engineering 2.0"
i = 2021
i = 10.55
r := i.(float64)
fmt.Println(r)
var i2 []interface{}
i2 = append(i2, "Go v2")
i2 = append(i2, 2022)
i2 = append(i2, 23.44)
i = map[int]string{
0: "GO",
1: "C",
}
i2 = append(i2, i)
for _, x := range i2 {
Generic(x)
}
}
func Generic(i interface{}) {
switch v := i.(type) {
case int:
fmt.Println("Int * 2=", v*2)
case float64:
fmt.Println("Float64/2=", v/2)
case string:
h := len(v) / 2
fmt.Println("text:", v, " - Quantidade/2 -> v[h:] + v[:h]=", v[h:]+v[:h])
case map[int]string:
for i, v := range v {
fmt.Println("key:", i, " valeu:", v)
}
default:
fmt.Println("isn't one of the types")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment