forked from GoesToEleven/GolangTraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
37 lines (31 loc) · 712 Bytes
/
type.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
package main
import "fmt"
// switch on types
// -- normally we switch on value of variable
// -- go allows you to switch on type of variable
type contact struct {
greeting string
name string
}
// SwitchOnType works with interfaces
// we'll learn more about interfaces later
func SwitchOnType(x interface{}) {
switch x.(type) { // this is an assert; asserting, "x is of this type"
case int:
fmt.Println("int")
case string:
fmt.Println("string")
case contact:
fmt.Println("contact")
default:
fmt.Println("unknown")
}
}
func main() {
SwitchOnType(7)
SwitchOnType("McLeod")
var t = contact{"Good to see you,", "Tim"}
SwitchOnType(t)
SwitchOnType(t.greeting)
SwitchOnType(t.name)
}