Skip to content

Instantly share code, notes, and snippets.

@meson10
Created December 14, 2014 15:13
Show Gist options
  • Save meson10/142d079910a36e36c528 to your computer and use it in GitHub Desktop.
Save meson10/142d079910a36e36c528 to your computer and use it in GitHub Desktop.
typecasting in go
package main
import "log"
func alterValue(val interface{}) {
INCR := 10
switch i := val.(type) {
case nil:
log.Println("Nil value")
case int:
log.Println(i + INCR)
case float64:
log.Println(i + float64(INCR))
default:
log.Println("Unknown Value")
}
}
func main() {
intVal := 10
floatVal := 10.4
alterValue(intVal)
alterValue(floatVal)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment