0

I have a directory outside of my GOPATH (I'm working on understanding Go modules; this is not yet a module, just a step along the way).

The directory path is ~/Development/golang/understanding-modules/hello.

The tree looks like:
hello/ (package main)

  • hello.go
  • hello_test.go
  • morestrings/ (package morestrings)
    • reverse.go
    • reverse_test.go

My function in reverse.go is:

// Package morestrings implements additional functions to manipulate UTF-8
// encoded strings, beyond what is provided in the standard "strings" package.
package morestrings

// ReverseRunes returns its argument string reversed rune-wise left to right.
func ReverseRunes(s string) string {
    r := []rune(s)
    for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
        r[i], r[j] = r[j], r[i]
    }
    return string(r)
}

My test function in reverse_test.go is:

package morestrings

import "testing"

func TestReverseRunes(t *testing.T) {
    got := ReverseRunes("Hello, World!")
    want := "!dlroW ,olleH"
    if got != want {
        t.Logf("Wanted %s, but got %s", want, got)
    }
}

The ReverseRunes function is showing the error undeclared name: ReverseRunes.

I'm following the set up/structure shown here & here.

Go version is 1.14.2 darwin/amd64

GO111MODULE is set to auto: GO111MODULE=auto, if that has any bearing.

I have tried reloading the VS Code window.

I have tried deleting the hello.go & hello_test.go files.

What does work is moving the morestrings directory up a level so that is in the same directory as hello: ~/Development/golang/understanding-modules/morestrings.

But the tutorials make it seem like ~/Development/golang/understanding-modules/hello/morestrings should work.

What am I missing?

skillit zimberg
  • 1,024
  • 1
  • 14
  • 22
  • 3
    Read How To Write Go Code and stick to that. And: Show the literal command you used on the command line and the verbatim output. – Volker Sep 25 '20 at 05:36
  • 2
    You cannot skip the module initialization step if you want to try modules. – Peter Sep 25 '20 at 06:33
  • 1
    If it's outside your GOPATH, it *must* be a module. There is no "step" between them. Just run `go mod init`. – Adrian Sep 25 '20 at 13:29
  • @Adrian, the error was showing in VS Code, not the terminal. I had been having trouble getting things to work while following How To Write Go Code, so I tried to back up to see if I was missing something else and went through a lot of additional materials trying to figure it out. Thank you! – skillit zimberg Sep 25 '20 at 17:29

1 Answers1

0

Packages in subdirectories have import paths consisting of the module path plus the path to the subdirectory.

The module path is specified in the go.mod file.

In this case your module path is ~/Development/golang/understanding-modules

So any package must be relative to there. If you want to move it elsewhere you need to give it its own module path. Otherwise that is where it must be. Or it must be imported as package hello/morestrings.

Sean F
  • 4,344
  • 16
  • 30
  • Considering that the "module path is specified in the go.mod file", how can you claim to know the module path considering the contents of the `go.mod` where not given (since, in fact, there is not one)? – Adrian Sep 25 '20 at 20:34
  • How can you claim there isn't one? – Sean F Sep 28 '20 at 04:17
  • Because they give the file structure and it doesn't include a go.mod, and because they said explicitly "this is not yet a module". – Adrian Sep 28 '20 at 13:52
  • No, they gave a list of source files and said nothing about whether it included everything. And the phrase "not yet a module" is obvious, that's why they're on stack overflow asking "why not?". So you're off-base and incorrect. – Sean F Sep 28 '20 at 15:26
  • And even if it *is* a module despite all evidence to the contrary, they still don't give the import path from their go.mod, so your answer stating "your module path is ~/Development/golang/understanding-modules" is making assumptions based on nothing. Import path is not file system path, it's whatever is defined in the module. – Adrian Sep 28 '20 at 15:31
  • There is no such thing as a "system" path, you appear to be making things up. My assumption was correct and based on the behavior as stated. – Sean F Sep 28 '20 at 21:27
  • I'm not making anything up. A file system path is a path to a node in a file system. For example, `~/Development/golang/understanding-modules` is such a path. – Adrian Sep 28 '20 at 21:32
  • Well of course it's not the file system path, it's never been, even before modules there was the gopath which still exists today. Why are you saying that file system path is the import path? That's wrong – Sean F Sep 28 '20 at 21:39
  • And we've come full-circle... I'm not saying the FS path is the import path; you are, in your answer, which is what kicked this whole discussion off. Anyway, good luck. – Adrian Sep 28 '20 at 21:53
  • No, you can read my answer, it hasn't changed, my answer says module path plus subdir path, which is correct. Only you make the false claim it's the file system path to something or other, you haven't even made that clear. – Sean F Sep 28 '20 at 21:59