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

Package import structure of maths unclear #693

Open
SubtleCo opened this issue Aug 4, 2023 · 4 comments
Open

Package import structure of maths unclear #693

SubtleCo opened this issue Aug 4, 2023 · 4 comments

Comments

@SubtleCo
Copy link

SubtleCo commented Aug 4, 2023

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
Copy link
Author

SubtleCo commented Aug 4, 2023

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
Copy link

rexroof commented Jun 9, 2024

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
Copy link

rexroof commented Jun 9, 2024

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.

@Yagisanatode
Copy link

Yagisanatode commented Jul 4, 2024

Thanks for the clues.

After spending most of the morning searching for a solution, I came up with this approach based on part 5 of the go.dev tutorial Call your code from another modue.

Here we can create two modules, one for testing and one for our clock. Then connect the clock module to the test module using a replace directive.

Note that the tutorial recommends that replace only be used to access a package locally and not to be used in published versions. The Math tutorial in "Learn go with tests" seems like a reasonable use case for this.

Steps to replicate

  1. Create a Math folder.

  2. Inside the Math folder create two more folders: clock and clockTest.

  3. Navigate to the clock folder in the terminal.

  4. In the terminal, run go mod init example.com/clockface where example.com is a 'dummy' URL. It seems that github.com/<yourproifilename>/<modulename> is the most common pattern representing a published path that Go tools can access.

  5. Create the file clockface.go in the clock folder. Add package clockface and save.

  6. Navigate to the clockTest folder in the terminal.

  7. In the terminal, run go mod init example.com/clocktest

We need to replace the example.com/clockface with the local path ../clock.

  1. Still in the folder clockTest in the terminal, run
    go mod edit -replace example.com/clockface=../clock

  2. Then run go mod tidy. The terminal should return something like this:
    go: found example.com/clockface in example.com/clockface v0.0.0-00010101000000-000000000000

  3. In your IDE your go.mod file should now look like this:

module example.com/clocktest

go 1.22.4

replace example.com/clockface => ../clock

require example.com/clockface v0.0.0-00010101000000-000000000000
  1. In your IDE in your clockTest folder create the file clockface_test.go.
  2. Add the first sample code for the Math tutorial and save.
  3. In the terminal run, go test. You should get:
.\clockface_test.go:13:20: undefined: clockface.Point
.\clockface_test.go:14:19: undefined: clockface.SecondHand
FAIL    yagitest.com/clocktest [build failed]

Matching the first error for the tutorial.

Final thoughts

This is a fantastic course and this is the only time that I really got stuck. A chapter on modules and packages would be a great precursor to the Math section.

Hope this helps.

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

3 participants