go – debugging a struct implementing an increment function

Exception or error:

I’m trying to implement an increment function on my struct. I’m not sure why, but it appears that when I call F.incr() in main, the fib struct’s parameters appear to remain constant. I think I may have an incorrect idea about what I’m doing in my incr() function, though I’m not able to find the right documentation. What am I missing?

type fib struct {
    i uint64 
    fa uint64   
    fb uint64 
}

func (F fib) incr(){
    F.i++   
    F.fa, F.fb = F.fa+F.fb, F.fa
}

func main() {
    F := fib{1,1,0}
    var sum uint64 = 0
    for; F.i <= 10; F.incr() {
        k := f(F.i, F.fb, F.fa)
        fmt.Printf("calculating the %vth f(i,F_%v, F_%v): %v\n", F.i, F.i-1, F.i, k)    
—snip-

edit: thanks @peterSO, I needed to change incr to
func (F *fib) incr(){

How to solve:

You need to update your method to increase the value of the variable at the location. Hence, you need to change the F() method

func (F *fib) incr(){
    F.i++   
    F.fa, F.fb = F.fa+F.fb, F.fa
}

The difference being the *

Leave a Reply

Your email address will not be published. Required fields are marked *