I try writing simple counter but I don't understand why he didn't work.. There is my code
package main
import "fmt"
type Count int
type Counter interface {
Next()
Prev()
Jump(j int) //i want increase Count to 'j' value
}
func (c *Count) Next() { *c += 1 }
func (c *Count) Prev() { *c -= 1 }
func (c *Count) Jump(j int) { *c += j } //Here Error
func main() {
val := new(Count) //0
val.Next() //+1
val.Jump(4) //+4
val.Prev() //-1
fmt.Println("Now ", *val) //expected 4
}
Is anybody knows what the problem here? Thanks for advance!
Simply change the Jump signature:
Jump(j Count)
And you will get the expected result.
See play.golang.org
If you don't, you would get:
prog.go:15: invalid operation: *c += j (mismatched types Count and int)
[process exited with non-zero status]