Hola!

Based on your initial project layout you have a couple of issues. When
using the legacy gopath everything should ideally live under that path.
Imports then become relative to that root.

Your main lives outside of the root which works with go run but is not how
a project should be structured.

For a small toy project I would place both files in the same directory and
set their package name to main.

If however your objective is to better understand imports then I would
reconsider your project structure. In the root directory of your project I
would place your main.go and run “go mod init” there. In that root
directory you can then create your jsonstuff directory. As an example:

project/main.go
project/go.mod
project/jsonstuff/types.go

When using go mod it typically (but not always) maps to a single
module/project/repository.

go mod init <module>

The module specified above is usually a URI without the protocol. By
following this practise it makes the project “go gettable”. If you maintain
src as your module name you would need to change the import for jsonstuff
to src/jsonstuff and move your main.go to the src directory.

As mentioned by others src isn’t a good name for a module. Better to do
something like:

go mod init github.com/<your account>/helloworld

You would then change your jsonstuff import to:

github.com/<your account>/helloworld/jsonstuff

Be aware only one package can live per directory except the special *_test
form which restricts test access to the packages public api only.

On Thu, Jul 30, 2020 at 23:55, Julian Fields <[email protected]>
wrote:

> Thanks Jesper and Volker.
> After reading the article(s) and go help modules and a few blogs, I still
> was not able to see why deleting go.mod makes the program import
> jsonstuff package, and compile and run?
> Modules are a great idea and I want to use them, but I can't get it to
> compile
> a "hello golang" program even:
>
> =======================
> $ ls  /home/jfields/go/src/jsonstuff
> typestuff.go
> $ go mod init src
> go: creating new go.mod: module src
> $ more go.mod
> module src
>
> go 1.14
> $ go run main.go
> main.go:7:2: package jsonstuff is not in GOROOT
> (/usr/local/go/src/jsonstuff)
> $ rm go.mod
> $ go run main.go
> Hello golang
> {0}
> =========================
>
>
> On Thu, Jul 30, 2020 at 5:01 AM Jesper Louis Andersen <
> [email protected]> wrote:
>
>> On Thu, Jul 30, 2020 at 12:54 PM Volker Dobler <
>> [email protected]> wrote:
>>
>>> You dop not import files and you do
>>> not run files and you do not test files. Files contain the
>>> sources but are basically uninteresting: Focus on
>>> packages (and modules).
>>>
>>
>> In Go, we disconnect the name of a file and the package in which it
>> lives. That is, the programmer is free (mostly) to choose whatever names
>> for files in a package, and also free to create as many files as is seen
>> necessary. It is in contrast to a large set of other languages, which
>> require that the package and the filename stay the same. The advantage is
>> that you can have very large packages and gracefully split them over
>> multiple files without having to resort to inventing new internal package
>> names.
>>
>> My general view is that you shouldn't assume a connection between the
>> file system and the packages of your language, and the languages which do
>> have the wrong design. However, since that "mistake" is made in many
>> languages, people tend to get somewhat confused when they encounter a
>> system where it isn't the case.
>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "golang-nuts" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/golang-nuts/pP-0WEpzbEs/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/golang-nuts/CAGrdgiX%2BN%2BospARM7adQ1E0wk-K7M73Jbj1KEUXkxjGGq9Q9zg%40mail.gmail.com
>> <https://groups.google.com/d/msgid/golang-nuts/CAGrdgiX%2BN%2BospARM7adQ1E0wk-K7M73Jbj1KEUXkxjGGq9Q9zg%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/CAAcmRO8TZ4ftK%2BFsTQTw7nEGZ0e3gFFpv5uygb4S6YavVRe_Xg%40mail.gmail.com
> <https://groups.google.com/d/msgid/golang-nuts/CAAcmRO8TZ4ftK%2BFsTQTw7nEGZ0e3gFFpv5uygb4S6YavVRe_Xg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
-- 
Nathan Fisher
 w: http://junctionbox.ca/

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CA%2Bc2dWdySi0JRs4HfWLxc4zHJco%3DrSdy7cspkyS4mreS2EEbgg%40mail.gmail.com.

Reply via email to