Last active
February 20, 2022 21:58
-
-
Save Patrikios/d0ac16a927b9481d6558687aed10b83c to your computer and use it in GitHub Desktop.
Gist on type checking 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 | |
// Sources: | |
// 1. https://go.dev/tour/methods/15 | |
// 2. https://go.dev/tour/methods/16 | |
// 3. https://stackoverflow.com/questions/6996704/how-to-check-variable-type-at-runtime-in-go-language | |
// 4. https://go.dev/tour/methods/9 | |
// Notions | |
// concrete types like int, float64, string etc | |
// interface{} types | |
import ( | |
"fmt" | |
"math" | |
"reflect" | |
) | |
func assert_type_switch(i interface{}) { | |
switch v := i.(type) { | |
case int: | |
fmt.Printf("Twice %v is %v\n", v, v*2) | |
case string: | |
fmt.Printf("%q is %v bytes long\n", v, len(v)) | |
default: | |
fmt.Printf("I don't know about type %T!\n", v) | |
} | |
} | |
type MyFloat float64 | |
type Vertex struct { | |
X, Y float64 | |
} | |
type EmptyInterface interface{} | |
type Abser interface { | |
Abs() float64 | |
} | |
func (v Vertex) Abs() float64 { | |
return math.Sqrt(v.X*v.X + v.Y*v.Y) | |
} | |
func (f MyFloat) Abs() float64 { | |
return math.Abs(float64(f)) | |
} | |
func main() { | |
// Variables to assert type of | |
var a, b, c = 3, 4.1, "foo" | |
var x interface{} = 123 | |
g := float32(43.3) | |
var ia, ib Abser | |
ia = Vertex{1, 2} | |
ib = MyFloat(1) | |
fmt.Println("---") | |
fmt.Println(a) | |
fmt.Println(b) | |
fmt.Println(c) | |
fmt.Println(x) | |
fmt.Println(g) | |
fmt.Println(ia) | |
fmt.Println(ib) | |
fmt.Println("---") | |
// simply printing the types | |
fmt.Printf("a is of type %T\n", a) | |
fmt.Printf("b is of type %T\n", b) | |
fmt.Printf("c is of type %T\n", c) | |
fmt.Printf("x is of type %T\n", x) | |
fmt.Println("---") | |
// Using type switch | |
assert_type_switch(a) | |
assert_type_switch(b) | |
fmt.Println("---") | |
// Assert type of a variable by simply dumping its type into a string | |
fmt.Println("The variable 'c' is of type 'string':", | |
fmt.Sprintf("%T", c) == "string") | |
fmt.Println("---") | |
// Assert interface underlying type | |
_, ok := x.(int) | |
if ok { | |
fmt.Println("The underlying value of interface variable 'x' is of type 'int': true") | |
} | |
fmt.Println("---") | |
// package reflect | |
// use on concrete as well on interface{} types | |
gt := reflect.TypeOf(g).Kind() | |
fmt.Printf("%T: %s\n", gt, gt) | |
if gt == reflect.Float32 { | |
println(">> g is float32") | |
} | |
// use on own types | |
// reflect.TypeOf(xxx) returns the direct types which you might want to use, while reflect.TypeOf(xxx).Kind() returns the basic types. | |
fmt.Println(reflect.TypeOf(ia)) | |
fmt.Println(reflect.TypeOf(ia).Kind()) | |
fmt.Println(reflect.TypeOf(ib)) | |
fmt.Println(reflect.TypeOf(ib).Kind()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment