-
Notifications
You must be signed in to change notification settings - Fork 0
/
Allocation.go
40 lines (36 loc) · 876 Bytes
/
Allocation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"fmt"
"reflect"
)
func main() {
num := new(int) // new create pointer
fmt.Println("Pointer is", num, "value is", *num)
*num = 1234
fmt.Println("after assign value, Pointer is", num, "value is", *num)
fmt.Printf("type of num is %T \n", *num)
// type checking
fmt.Println("type of *num 1st way", firstWay(*num))
fmt.Println("type of *num 2nd way", secondWay(*num))
fmt.Println("type of *num 2nd way", thirdWay(*num))
}
// three ways to check type
// N.B: empty interface means any value
func firstWay(v interface{}) string {
return fmt.Sprintf("%T", v)
}
// using built-in reflect package
func secondWay(v interface{}) string {
return reflect.TypeOf(v).String()
}
// using type switch
func thirdWay(v interface{}) string {
switch v.(type) {
case int:
return "int"
case string:
return "string"
default:
return "unknown"
}
}