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 Structs

laura gaber
laura gaber
4,622 Points

Why won't my function work?

Please help me understand why my code won't work.

src/clock/clock.go
package clock

type Clock struct {
  Hours int
  Minutes int
}

func main(){
  Noon(12, 0)
}

// DEFINE A "Noon" FUNCTION HERE
func Noon(hours int, minutes int) newClockVal Clock{
  return newClockVal := Clock{Hours: hours, Minutes: minutes}
} 

1 Answer

daniel104729
daniel104729
7,176 Points

Next time please include a sentence or two on what you are trying to do, the error, and what you have tried

There are a few issues I notice, you are using a main function but not a main package. The main function will not execute automatically if you not using the main package.

The code will not compile because there a few syntax issues with your function.

If you are going to do a named return value then you need to wrap it in parentheses like

func Noon(hours int, minutes int) (newClockVal Clock) {

but then you do not want to use := just use = because newClockVal is already defined and allocated. Also you want to have the return statement on a separate line or else it will cause a compile error. It should look like this

func Noon(h int, m int) (ncv Clock) {
ncv = Clock{Hours: h, Minutes: m}
return
}

I suppose you may be trying to do it this way instead, I prefer this way myself

func Noon(h int, m int) Clock{
return Clock{Hours: h, Minutes: m}
}

Even when your code compiles it will output nothing, you need to use fmt

Also, I do not understand the name Noon. If you can define anytime then the name Noon doesn't make sense.

Noon would make sense if you do something like this

func Noon() Clock{
return Clock{Hours: 12, Minutes:0}
}