-3

I am new to go module building. What im trying to do is to create a go library with commonly used methods that other developers can use in their go code.

So first in my internal github, Ive created a repo https://internalgithub/<org>/lib-go.git

The repo structure looks like

$  tree
.
├── README.md
├── go.mod
├── go.sum
└── main.go

And the main.go file has the following content

package main

import (
    "flag"
    "os"
    "path/filepath"

    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/rest"
    "k8s.io/client-go/tools/clientcmd"
)

func main() {}

func someFunctinDevelopersNeed(cluster bool) kubernetes.Interface {
    if cluster == false {
        var kubeconfig *string
.
.
    return clientset
}

The code is available in the master branch of my org github.

The next task to to write code that invokes this library and in turn invokes the methid someFunctinDevelopersNeed

So in my app code, my main.go looks like

package main

import "internalgithub/<org>/lib-go"


func main() {
    clientset = someFunctinDevelopersNeed()
.
.
}

But when I try to do go get -t -v internalgithub/<org>/lib-go, I get

runtime.main_main·f: function main is undeclared in the main package

Is there something I have missed ?

EDIT:

Based on suggestions, now I have done the following

$ tree
.
├── README.md
├── go.mod
├── go.sum
└── kubernetes
    └── kubernetes.go

And kubernetes.go starts with

package kubernetes

import (
    "flag"
    "os"
    "path/filepath"

    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/rest"
    "k8s.io/client-go/tools/clientcmd"
)

func someFunctinDevelopersNeed(cluster bool) kubernetes.Interface {
    if cluster == false {
        var kubeconfig *string
.
.
    return clientset
}

Even with these changes, I run into the same error


Jason Stanley
  • 386
  • 1
  • 3
  • 20
  • 2
    You can't import a `main` package. Put the shared code in a different package. – Adrian Sep 21 '20 at 22:17
  • https://stackoverflow.com/questions/25318589/why-cant-i-add-a-main-to-my-library-in-golang?rq=1 appears to be very similar to your issue but possibly not a true duplicate. – Adrian Sep 21 '20 at 22:18
  • 1
    Just to make sure Adrian correct comment is not misinterpreted under the influence of the answers. A package main is perfectly fine in a module (even multiple package main are common) but it is _totally_ _impossible_ to import any package main. Code you intend to be reused _must_ be put in any other package. – Volker Sep 22 '20 at 06:20
  • @Adrian I have edited my question with your suggestion and implemented it too. Im running into the same issue – Jason Stanley Sep 22 '20 at 14:37
  • @JasonStanley please check your edit, as it seems to have been cut off, and does not include the full changes made nor the error you're getting with the changes. – Adrian Sep 22 '20 at 17:29

1 Answers1

-3

If you write your own library, do not add main package.

You can see some popular golang libraries, like go-gin(uses gin.go ), gorm (uses gorm.go)

For your case, if you just want to create a collection for most commonly used functions, you can add a package utils and put your implementation into utils.go or other xxx.go files

oscarz
  • 1,184
  • 11
  • 19