quii / learn-go-with-tests

Learn Go with test-driven development
MIT License
21.55k stars 2.75k forks source link

Package import structure of maths unclear #693

Open SubtleCo opened 11 months ago

SubtleCo commented 11 months ago

Digging around, I see there's been some conversation about how the package titles suddenly changed, and we now have two packages, one called package clockface and one called package clockface_test. The sample online has the projectpath as a GitHub URL, which may be normal but up to this point, we haven't been introduced to this style of import.

I ended up making it work with both a go mod init clockface and a go work init . which got my linter to play nice, but I'm not 100% sure on what I did.

I think if you're going to change up the import style from what's been consistent before this chapter, an explanation and instruction is called for, especially for those of us using this track to learn Go for the first time.

THANK YOU all! This has been a wonderful experience so far

SubtleCo commented 11 months ago

Thinking about it a bit more - I think it just didn't click in my head that what I name my go mod at the root project level had the effect of defining package paths.

I ended up deleting my local go mod and go work files from the maths dir and edited my go.mod at the root (in my case, go_with_tests/) to say "module go_with_tests"

This allowed me in the test file to use import "go_with_tests/maths" given that I had a file clock.go with package clockface at the top of the file.

Anyway, all this to say - I'm nitpicking. This tutorial has been so wonderfully self sufficient, and this was my first real hiccup. It would be cool to smooth over, and I wonder if that happens in ch. 1?

rexroof commented 3 weeks ago

thank you for this! this helped me moved forward in this chapter. i was stuck for ages at the start of this chapter because I couldn't make heads or tails of how my local folder should be set up. I even downloaded the example source and couldn't figure out how to reflect the changes in my local code.

rexroof commented 3 weeks ago

This stopped working during the second phase of this chapter:

$ go test
# go-with-tests/maths_test [go-with-tests/maths.test]
./clockface_test.go:7:3: "go-with-tests/maths" imported as clockface and not used
./clockface_test.go:13:10: undefined: secondsInRadians
FAIL    go-with-tests/maths [build failed]

I notice the code in the second phase doesn't prefix the functions with clockface. anymore. Also, isn't a lower case function not going to be exported from the package?

here is my code, currently:

$ cat clockface.go
package clockface

import "time"

type Point struct {
        X float64
        Y float64
}

func SecondHand(t time.Time) Point {
        return Point{150, 60}
}

func secondsInRadians(t time.Time) float64 {
        return 1.1
}

$ cat clockface_test.go
package clockface_test

import (
  "math"
  "testing"
  "time"
  "go-with-tests/maths"
)

func TestSecondsInRadians(t *testing.T) {
  thirtySeconds := time.Date(312, time.October, 28, 0, 0, 30, 0, time.UTC)
  want := math.Pi
  got := secondsInRadians(thirtySeconds)
  if got != want {
    t.Errorf("Got %v radians, wanted %v", got, want)
  }
}

changing it to start with a capital letter and changing it to clockface.SecondsInRadians() appears to work.