forked from steger/pr3-ws202526
27 lines
565 B
Go
27 lines
565 B
Go
package main
|
|
|
|
import "fmt"
|
|
|
|
type Counter struct {
|
|
value int
|
|
}
|
|
|
|
// pointer receiver. The method operates directly on the object c points to
|
|
// Use a pointer receiver whenever the object needs to be modified.
|
|
func (c *Counter) Increment() {
|
|
c.value++
|
|
}
|
|
|
|
// value receiver. The method operates on copy of object c. Use a value
|
|
// receiver when the object does not need to be modified in the method.
|
|
func (c Counter) String() string {
|
|
return fmt.Sprintf("Counter value: %d", c.value)
|
|
}
|
|
|
|
func main() {
|
|
c := Counter{}
|
|
fmt.Println(c)
|
|
c.Increment()
|
|
fmt.Println(c)
|
|
}
|