Problem in using go modules and custom go packages at the same time

Hey everyone, I’m currently working on a project where I am using client-go library (for Kubernetes) and I have my code ready, but I want to modify the code properly so I want the function that uses client-go and K8s APIs to be under a package. I tried implementing it using custom packages.

Golang version: go version go1.16.4 linux/amd64
Since go modules is by default ‘ON’ for this version so I use GO111MODULE=off go run main.go

Currently my working directory looks something like this,
|-WorkingDirectory
|–main.go
|–go.mod
|–go.sum
and main.go works perfectly in this case, I have to use go run main.go (since if I use GO111MODULE=off go run main.go in this case then it throws an error of- cannot find package k8s.io/client-go/tools/clientcmd)

And as I want all the client-go functions in a package, I tried implementing this file structure,
|-WorkingDirectory
|–main.go
|–test(the package name)
|—go.mod
|—go.sum
|—test1.go

But in this case, I am stuck.
As if I use go run main.go then as I am using go1.16.4 so I am able to use custom packages whereas if I use GO111MODULE=off go run main.go then I can enjoy using custom packages but the client-go and Kubernetes APIs won’t work as I am using go.mod for them.

So can anyone please help me out of this trap and guide me how can I have a custom package that has all the functions which uses client-go and Kubernetes APIs.

Why do you have a nested module instead of just a nested package (without its own go.mod)?

@skillian can you please explain. It would be nice if you can tell me how the file structure will look like

Your folder structure looks OK, I think. I can’t quite see which files nest where because your ASCII-art folder structure was formatted by the forum. Try wrapping code and text that should be in a fixed-width font in three backticks (```), for example:

```
code/text here
```

I mean that the go.mod and go.sum files should probably be at the top level, not nested within your test package.

@skillian Ok sorry see this is the file structure that I was talking about

|-WorkingDirectory
|--main.go
|--test(the package name)
|---go.mod
|---go.sum
|---test1.go

I mean that the go.mod and go.sum files should probably be at the top level, not nested within your test package.

So is this how you think the structure should look like?

|-WorkingDirectory
|--main.go
|--go.mod
|--go.sum
|--test(the package name)
|---test1.go

But my test1.go is also using some modules like K8s APIs and client-go libraries which were mentioned in go.mod and thats why I kept go.mod and go.sum inside my test package.
Will this new file structure support my requirement?

I think so. A module is a collection of packages that are versioned together, so your working directory is a package and your test folder is a package but they together form a module rooted in your working directory because you have a go.mod file there. This is how I’ve written my own modules.

Yes actually I solved the problem. We have to use go modules in that case and for that we should use the following file structure

|-WorkingDirectory
|--main.go
|--go.mod
|--go.sum
|--test(the module name)
|---test1.go
|---go.mod
|---go.sum

The go.mod inside test module will have the modules required for the functions used inside test module whereas the go.mod outside the test module will have reference to our test module.
Reference links- https://golang.org/doc/tutorial/create-module
https://golang.org/doc/tutorial/call-module-code

Thank you for the help @skillian

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.