Skip to content

Instantly share code, notes, and snippets.

@torufurukawa
Created February 4, 2017 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torufurukawa/0f01b6ec35ee56f37acd105028ec1609 to your computer and use it in GitHub Desktop.
Save torufurukawa/0f01b6ec35ee56f37acd105028ec1609 to your computer and use it in GitHub Desktop.
go の動的型変換
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