MCPcopy Create free account
hub / github.com/goinaction/code / main

Function main

chapter10/listing05/listing05.go:48–81  ·  view source on GitHub ↗

=============================================================================

()

Source from the content-addressed store, hash-verified

46// =============================================================================
47
48func main() {
49
50 // Declare variables of the MoveLocker and Mover interfaces set to their
51 // zero value.
52 var ml MoveLocker
53 var m Mover
54
55 // Create a value of type bike and assign the value to the MoveLocker
56 // interface value.
57 ml = bike{}
58
59 // An interface value of type MoveLocker can be implicitly converted into
60 // a value of type Mover. They both declare a method named move.
61 m = ml
62
63 // prog.go:65: cannot use m (type Mover) as type MoveLocker in assignment:
64 // Mover does not implement MoveLocker (missing Lock method)
65 ml = m
66
67 // Interface type Mover does not declare methods named lock and unlock.
68 // Therefore, the compiler can't perform an implicit conversion to assign
69 // a value of interface type Mover to an interface value of type MoveLocker.
70 // It is irrelevant that the concrete type value of type bike that is stored
71 // inside of the Mover interface value implements the MoveLocker interface.
72
73 // We can perform a type assertion at runtime to support the assignment.
74
75 // Perform a type assertion against the Mover interface value to access
76 // a COPY of the concrete type value of type bike that was stored inside
77 // of it. Then assign the COPY of the concrete type to the MoveLocker
78 // interface.
79 b := m.(bike)
80 ml = b
81}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected