gokubernetesmakefilepackagevendor

cannot find package "k8s.io/metrics/pkg/client/clientset/versioned" in any of:Vendor , GOROOT , GOPATH


I am trying to create i scheduler so after writing the code and creating deployment i use a make file to build and use vendor also but when i use my first code which use the same imports as the code in github reposetory it works but when i add to it and use k8s.io/metrics/pkg/client/clientset/versioned as import it give me an error:

    cmd/scheduler/main.go:24:5: cannot find package "k8s.io/metrics/pkg/client/clientset/versioned" in any of:
    /go/src/github.com/username/scheduler/vendor/k8s.io/metrics/pkg/client/clientset/versioned (vendor tree)
    /usr/local/go/src/k8s.io/metrics/pkg/client/clientset/versioned (from $GOROOT)
    /go/src/k8s.io/metrics/pkg/client/clientset/versioned (from $GOPATH)

makefile:

    SHELL = /bin/bash
    OS = $(shell uname -s)
    PACKAGE = github.com/username/scheduler
    BINARY_NAME = scheduler
    IMAGE = name
    TAG = tagsvalue
    
    BUILD_DIR ?= build
    BUILD_PACKAGE = ${PACKAGE}/cmd/scheduler
    DEP_VERSION = 0.5.0
    GOLANG_VERSION = 1.11

    .PHONY: clean
    clean: ## Clean the working area and the project
           rm -rf bin/ ${BUILD_DIR}/ vendor/
           rm -rf ${BINARY_NAME}

    bin/dep: bin/dep-${DEP_VERSION}
           @ln -sf dep-${DEP_VERSION} bin/dep
   bin/dep-${DEP_VERSION}:
            @mkdir -p bin
            curl https://raw.githubusercontent.com/golang/dep/master/install.sh |   INSTALL_DIRECTORY=bin DEP_RELEASE_TAG=v${DEP_VERSION} sh
            @mv bin/dep $@

    .PHONY: vendor
    vendor: bin/dep ## Install dependencies
           bin/dep ensure -v -vendor-only

    .PHONY: build
    build: ## Build a binary
            go build ${BUILD_PACKAGE}

please help i know that the question is not to clear but i am a new to golang so any information will help. thank you


Solution

  • Alright, seeing as you want to know how to use go modules, I'll write a brief summary. The full documentation is here, on the go.dev site. It's easy to find, and walks you through the entire process beginning to end.


    I have been using go as my main language since go 1.4. Since the introductions of go modules, I can honestly say that I rarely, if ever, need a Makefile. To use modules is fantastically easy:

    $ cd ~
    $ mkdir new_project
    & cd new_project
    # initialise module
    $ go mod init github.com/user/new_project
    $ ls
    go.mod
    $ cat go.mod
    module github.com/user/new_project
    
    go 1.19
    # the version in the mod file will reflect the version of go you have installed locally
    

    Now to add dependencies:

    $ go get github.com/jinzhu/copier
    # check:
    $ ls
    go.mod    go.sum
    $ cat go.mod
    module github.com/user/new_project
    
    go 1.19
    
    require github.com/jinzhu/copier v0.3.5 // indirect
    

    The dependency was added, and a go.sum file is created. This acts as a lock file for your dependencies. It's basically the commit hash of the exact version used in your project.

    You can specify a specific version (saw we want version v0.3.2 instead of 0.3.5), you can just use the command:

    $ go get github.com/jinzhu/copier@v0.3.2
    

    Or you can manually add dependencies to your mod file in your editor:

    module github.com/user/new_project
    
    go 1.19
    
    require (
       github.com/jinzhu/copier v0.3.2
       github.com/foo/bar
    )
    

    And so on. Check the docs for things like replace and require_test, and what they do.

    Now you can just compile your code:

    $ go build .
    

    Any dependencies that you haven't downloaded yet will be automatically checked and updated/downloaded for you. If you want to download the dependencies manually, you can:

    $ go mod download
    

    If you want to remove dependencies that are no longer in use (ie clean up the go.sum file):

    $ go mod tidy
    

    There's nothing more to it, really.