GoLang custom package import problem

王林
Release: 2024-02-12 21:54:08
forward
786 people have browsed it

GoLang custom package import problem

php editor Xinyi is here to introduce to you a common problem in GoLang: custom package import problem. In Go language development, we often need to use custom packages to implement some specific functions. However, when importing custom packages, some problems sometimes occur, such as package cannot be found, package name conflicts, etc. This article will answer these questions in detail and provide solutions to help developers better handle custom package import problems and improve development efficiency.

Question content

I am learning golang and encountered a problem.

I used go mod init main to create the mod file

Next I created the controller and routing folders as shown below:

├── contollers
│   └── users.controller.go
├── routes
│   ├── index.go
│   └── users.routes.go
├── vendor
│   └── modules.txt
├── go.mod
├── go.sum
└── main.go
Copy after login

In the mod file, the module looks like this Module main

Now when I try to import the controller into the router it gives me an import error.

I have been doing the following. Try - 1

import (
    "$gopath/controllers"

    "github.com/gin-gonic/gin"
)
Copy after login

It gives invalid import path: "$gopath/controllers" syntax error

Try - 2

import (
    "$gopath/main/controllers"

    "github.com/gin-gonic/gin"
)
Copy after login

Same error

Try - 3

import (
    "main/controllers"

    "github.com/gin-gonic/gin"
)
Copy after login

Controller.go

package controllers;

import (
    "fmt"

    "github.com/gin-gonic/gin"
)

func healthcheck() gin.handlerfunc {
    return func (c *gin.context)  {
        fmt.println("reached controller")
    }
}
Copy after login

router.go

package routes

import (
    "bootcamp.com/server/controllers"

    "github.com/gin-gonic/gin"
)

func UserRouters(inComingRoutes *gin.Engine) {
    inComingRoutes.GET("/api/health", controllers.HealthCheck());
}
Copy after login

Throws this error, Unable to import main/controllers (no required module provides package "main/controllers")

I've been stuck with this issue for 3-4 hours, can someone please suggest me how to import this controller into my routes.

Thanks in advance.

Solution

  1. Modify the module path by editing go.mod:
- module main
+ module example.com/hello
Copy after login
  • Modify the import path:
  • import (
    -     "main/controllers"
    +     "example.com/hello/controllers"
    
          "github.com/gin-gonic/gin"
      )
    Copy after login
  • controller.go (remove trailing ;):
  • - package controllers;
    + package controllers
    Copy after login
    1. Rename directory contollers to controllers to match the package name (missing r).

    2. Delete the vendor folder.

    illustrate:

    1. main has a special meaning in go. Quoted from golang specification:

    A complete program is created by transitively linking a single unimported package named main package with all its imported packages. The main package must have the package name main and declare a function main that takes no parameters and returns no value.

    1. Import paths without dots are reserved for the standard library and go toolchain. See documentation cmd/go: Preserving module names without dots.

    The above is the detailed content of GoLang custom package import problem. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:stackoverflow.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!