Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConfiurableSleeper in Mocking needs a New-Method #659

Open
fonzerelly opened this issue Apr 18, 2023 · 0 comments
Open

ConfiurableSleeper in Mocking needs a New-Method #659

fonzerelly opened this issue Apr 18, 2023 · 0 comments

Comments

@fonzerelly
Copy link
Contributor

fonzerelly commented Apr 18, 2023

Hi Chris,
thanx a lot for this book. I love it with Passion!

I'm working through it already the second time and probably it has to do with my missunderstanding, how modules work in go, but I ran into the same error even the second time: I started to create a new module for each chapter since my impression was that each chapter must not correlate with the other. (In your reference implementation, everything is just part of the main module). As a result, when I try to compile this code in my hello.go main module (since I run into errors when I try to create several main functions)

package main

...

func main () {
   sleeper := &mocking.ConfigurableSleeper{1 * time.Second, time.Sleep}
   mocking.Countdown(os.Stdout, sleeper)
}

I run into this error:

hello_world\hello.go:62:42: implicit assignment to unexported field duration in struct literal of type mocking.ConfigurableSleeper
hello_world\hello.go:62:59: implicit assignment to unexported field snooze in struct literal of type mocking.ConfigurableSleeper

This was quite iterating for me at first, because in the tests it was possible to set those fields directly but not from the main module. I solved the issue by this

in mocking.go

package mocking

...

func (cs *ConfigurableSleeper) New (duration time.Duration, snooze func(time.Duration)) {
	cs.duration = duration
	cs.snooze = snooze
}

and in hello.go

package main

...

func main() {
	sleeper := &mocking.ConfigurableSleeper{}
	sleeper.New(1 * time.Second, time.Sleep)

	mocking.Countdown(os.Stdout, sleeper)
}

What do you think? Would it be helpful to mention that you can not access private struct memebers accross module borders and therefore have to implement something like a constructor? Or was I completely wrong in setting up every chapter as a module on its own and explain how to work with more then one main module in the modules-chapter? Maybe it would also make sense to explain when to split a project into several modules?

Thanx a lot for your Feedback
Fonzy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant