Created
December 14, 2014 15:13
-
-
Save meson10/142d079910a36e36c528 to your computer and use it in GitHub Desktop.
typecasting in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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