Add is a mutation operation. If we go with the idea that we should be using pointer semantic when we mutate something and value semantic when we don't then Add is implemented wrong. However, it has not been wrong because it is the type that has to drive the semantic, not the implementation of the me
(d Duration)
| 97 | // Add is using a value receiver and returning a value of type Time. It is mutating its local copy |
| 98 | // and returning to us something new. |
| 99 | func (t Time) Add(d Duration) Time { |
| 100 | t.sec += int64(d / 1e9) |
| 101 | nsec := int32(t.nsec) + int32(d%1e9) |
| 102 | if nsec >= 1e9 { |
| 103 | t.sec++ |
| 104 | nsec -= 1e9 |
| 105 | } else if nsec < 0 { |
| 106 | t.sec-- |
| 107 | nsec += 1e9 |
| 108 | } |
| 109 | t.nsec = nsec |
| 110 | return t |
| 111 | } |
| 112 | |
| 113 | // div accepts a value of type Time and returns values of built-in types. |
| 114 | // The function is using value semantics for type Time. |