Trying to create a test app for a library package

115 views
Skip to first unread message

Mark

unread,
Nov 2, 2022, 4:41:24 AM11/2/22
to golang-nuts
I have this layout:
```
mylib/
mylib/go.mod # module github.com/me/mylib
mylib/mylib.go
mylib/mylib_test.go
```
All this works fine, with both .go files being in the same pkg: mylib.

However, there are some tests that I can't really do using the test module because they write to stdout. So I want to create an exe for regression testing.

In particular I want the regression tester to be in package main (just like an app that uses mylib).

I've tried this layout:
```
mylib/
mylib/go.mod # module github.com/me/mylib
mylib/mylib.go
mylib/mylib_test.go
mylib/tester/tester.go
```
But I can't get the import to work:
```go
package main

import (
    "fmt"
    "mylib"
)

func main() {
    parser := mylib.Parser()
    fmt.Println(parser.AppName(), parser.Version())
}
```
The error I get is `tester/tester.go:8:2: package garg is not in GOROOT`, which is perfectly correct.
So then I tried to change the import to `../mylib`, but that also produces an error, `tester/tester.go:8:2: "../mylib" is relative, but relative import paths are not supported in module mode`

Is what I'm trying to do possible? If so, how? If not, what d'you recommend?

Brian Candler

unread,
Nov 2, 2022, 5:26:35 AM11/2/22
to golang-nuts
In mylib/tester/tester.go:

import (
    "fmt

Mark

unread,
Nov 2, 2022, 5:34:31 AM11/2/22
to golang-nuts
That doesn't work either (obviously I used my real github a/c name)
tester.go:8:2: package github/me/mylib is not in GOROOT

Mark

unread,
Nov 2, 2022, 5:38:01 AM11/2/22
to golang-nuts
I suppose what I'm really asking is this:

Given that I have a local pkg, mylib, whose module name is github.com/me/mylib, how can I create a local Go application that uses mylib from the local folder it is in rather than actually downloading it from github. (I do do the latter, but for quick testing & development that's too slow; I need to save an edit to mylib in one window & run myapp using the just edited mylib in another).

Mark

unread,
Nov 2, 2022, 7:50:54 AM11/2/22
to golang-nuts
I solved this problem by adding an extra go.mod file:
```
mylib/
mylib/go.mod # module github.com/me/mylib
mylib/mylib.go
mylib/mylib_test.go
mylib/tester/tester.go
mylib/tester/go.mod
```
This allowed me to change the import in tester.go to `import "mylib"`.
The text of tester/go.mod is:
```
module tester
go 1.19
require mylib v0.3.0
replace mylib v0.3.0 => ../../mylib
```

Brian Candler

unread,
Nov 2, 2022, 8:12:43 AM11/2/22
to golang-nuts
> tester.go:8:2: package github/me/mylib is not in GOROOT

That looks like a typo: "github" instead of "github.com"

You definitely don't need a second go.mod, nor do you need any "replace" statements.  The following works (literally "github.com/me/mylib" is fine too)

-- mylib/go.mod --
module github.com/me/mylib

go 1.18

-- mylib/mylib.go --
package mylib

import "fmt"

func Flurble() {
    fmt.Println("Flurble")
}

-- mylib/tester/tester.go --
package main

import (
    "github.com/me/mylib"
)

func main() {
    mylib.Flurble()
}


$ cd tester/
$ go run .
Flurble

Brian Candler

unread,
Nov 2, 2022, 8:15:03 AM11/2/22
to golang-nuts
> Given that I have a local pkg, mylib, whose module name is github.com/me/mylib, how can I create a local Go application that uses mylib from the local folder it is in rather than actually downloading it from github

FYI, what I've just shown doesn't download anything from github.  It works even with a random module name like "github.com/me/mylib" which doesn't match any real repository in github.

It walks up the tree to find go.mod, realises it already has github.com/me/mylib available locally, and uses that.

Mark

unread,
Nov 2, 2022, 8:28:37 AM11/2/22
to golang-nuts
Sorry, my mistake :-(
You are *right*. Doing it your way "just works".
Thank you :-)
Reply all
Reply to author
Forward
0 new messages