0

I want to use a function from a parent module for this project https://github.com/eregnier/beuss

however (and for the repository in the current state), all my tries fails.

in my root folder I have the following go.mod file

module github.com/eregnier/beuss

go 1.17

in the ./cmd folder I have the following go.mod file

module beuss/cmd

go 1.17

I tried various combination of go.mod file for the cmd folder like

module github.com/eregnier/beuss/cmd

go 1.17

I also tried some imports from the https://github.com/eregnier/beuss/blob/main/cmd/cmd.go file looking like

import (
    "fmt"
    "io/ioutil"
    "log"
    "os"
    "github.com/eregnier/beuss"
//  The following line does not work either
//  "github.com/eregnier/beuss" beuss
)

//try to use parent module functions

func main () {
    connPUT, err := beuss.newClient(beuss.MESSAGE_PUT)
}

nothing work.

I tried the following commands desperately

  • go mod init
  • go mod tidy
  • go get github.com/eregnier/beuss
  • go install github.com/eregnier/beuss

I got the follwing errors :

go get: github.com/eregnier/beuss@v0.0.0-20220101172233-d7ecaadb1d81: parsing go.mod:
    module declares its path as: beuss
            but was required as: github.com/eregnier/beuss

with not understanding the real issue. I googled errors, I had a look at this which looks intersting https://go.dev/doc/code#Workspaces

In the end, I am just loosing patience for this issue where I already spend lot of time without really understanding the real good practice / why it should be done like this or that.

I am sorry if this is a redundant question. but this topic looks nebulous to me where I just want to resolve a dependency. I guess I am lost between local imports / remote imports / modules and pacakges concepts. I guess I just want an entry point to understand all of this where in javascript a simple require('../code.js') does the trick.

What I would like in the end is to be able to better understand go imports system, and pragmatically, how to solve my linked project dependency so I can use functions from parent "folder" (ideally without namespacing)

utopman
  • 581
  • 4
  • 13
  • 2
    I'm sorry for not answering the question, but do you really need a nested module? Can you just remove the mod file from the ./cmd directory? Your reference to the parent package looks OK to me: import "github.com/eregnier/beuss". – Andrey Dyatlov Jan 02 '22 at 00:54
  • 2
    Also, this answer might be helpful, I think: https://stackoverflow.com/questions/57806081/how-to-access-nested-modules-submodules-in-go – Andrey Dyatlov Jan 02 '22 at 00:57
  • 1
    I’m not sure where to start, but it’s hard to tell where you’ve gone astray from these random commands. I would suggest going back through “How to Write Go Code” in detail, which is a step by step example of how to do this. – JimB Jan 02 '22 at 00:57

1 Answers1

0

As the question was unclear because I did not understand what was wrong, I'll try here to synthetise what worked for me.

I followed andrey dyatlov advice by simply removing the go.mod from my cmd folder and It is possible to make this folder a standalone program without module management. So I can go run / build the command file.

Also I slightly changed imports system and reorganized my code folders by understanding how imports works a bit better (I did not practised go since a while and I am a relatively new commer to go world)

So for this last point, doing imports with github.com/eregnier/beuss does the trick, but I missed to export my "lib" functions by just capitalizing their name. Doing so let me import and use them elsewhere in the code.

For the next person that this question might help, it should be possible to see the state of the project (wip) from the link in the question to see how it works now.

utopman
  • 581
  • 4
  • 13