Created
February 4, 2017 05:29
-
-
Save torufurukawa/0f01b6ec35ee56f37acd105028ec1609 to your computer and use it in GitHub Desktop.
go の動的型変換
This file contains hidden or 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 ( | |
"fmt" | |
"math/rand" | |
) | |
func main() { | |
var p Point = Point{2, 3} | |
var q Quality = Quality{99.9} | |
fmt.Println(p, q) | |
var x Any | |
fmt.Println(rand.Intn(2)) | |
if rand.Intn(2) == 1 { | |
x = p | |
} else { | |
x = q | |
} | |
v, ok := x.(Quality) | |
if !ok { | |
fmt.Println("Not a Quality") | |
} | |
fmt.Println(v) | |
} | |
type Any interface{} | |
type Point struct { | |
X int | |
Y int | |
} | |
type Quality struct { | |
Value float64 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment