Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Go Language Go Language Overview Custom Types Methods

akhter ali
akhter ali
15,778 Points

Increment not working properly

Locally this works without issues however the interpreter is complaining that the value of hours is not incrementing and I'm not sure why.

This is what I have locally to test

package main

import (
    "fmt"
)

type Hours int

func (h *Hours) Increment() Hours {
  return (*h + 1) % 24
}

func main() {
    hour := Hours(6)
    hourIncramented := hour.Increment()
    fmt.Println(hourIncramented)
}

This is from the treehouse editor.

src/clock/hours.go
package clock

type Hours int

func (h *Hours) Increment() Hours {
  return (*h + 1) % 24
}

3 Answers

Steven Parker
Steven Parker
229,744 Points

You haven't completed the necessary changes yet.

"Locally this works without issues" because you're still expecting a return value from the function and using it. But that's a different test criteria than the challenge requirements, which say to *"Convert Increment to ... modify the receiver directly".

So for the challenge you have a few conversion steps to do yet:

  • the instructions said to "modify the value at that pointer" — you still need to modify the original value
  • your new return won't need a value
  • your function declaration must be changed to reflect that there is no longer an Hours being returned
akhter ali
akhter ali
15,778 Points

This was completed in this order, in case it helps anybody.

  • your function declaration must be changed to reflect that there is no longer an Hours being returned -- Keep in mind, there is nothing to be returned if pointer values are being updated, you'll need to remove the Hours return value.
  • your new return won't need a value -- Removed return value due to pointer modification of (h)
  • the instructions said to "modify the value at that pointer" — you still need to modify the original value -- Changed declarations to pointer values so it doesn't copy a new (watch 4:40 - end of the video for an example)
Chufan Xiao
Chufan Xiao
18,955 Points

You shouldn't return the changed value, instead, you should assign the changed value to h like

*h=(*h + 1) % 24
func (h *Hours) Increment() {
  *h = (*h + 1) % 24
}