简体   繁体   English

在 Go 中导入自定义包

[英]importing custom package in Go

I checked out this answer and for some reason either I was unable to comprehend properly or it didn't work查看了这个答案,但出于某种原因,我无法正确理解或不起作用

Also, Before I start, I got that How we can do it with github but I want to try it without github另外,在我开始之前,我知道我们如何用 github 做到这一点,但我想在没有 github 的情况下尝试它

To start let's say I have a main.go file首先假设我有一个 main.go 文件

package main

import (
    "fmt"
    "math"
    "subpack"
)

//You Import packages without using comma in Go, rather space or new line
//In VS Code, if you use aren't using the package and run then it will automatically removie it

func main() {
    fmt.Println("hello world")
    //We use math.Floor to round the nunmber
    fmt.Println(math.Floor(2.7))
    fmt.Println(math.Sqrt(16))
    fmt.Println(subpack.Reverse)
}

Notice subpack here, it is the custom package I made.通知subpack在这里,它是定制包我做了。 The subpack exsist like this子包像这样存在

在此处输入图片说明

And contains the following code并包含以下代码

package subpack

//If we make this in the same root level of our main it will throw an error

func Reverse(s string) string {
    runes := []rune(s)
    for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
        runes[i], runes[j] = runes[j], runes[i]
    }
    return string(runes)
}

running our go is throwing following error运行我们的 go 抛出以下错误

cannot find package "subpack" in any of:
        /usr/local/go/src/subpack (from $GOROOT)
        /Users/anilbhatia/go/src/subpack (from $GOPATH)

Question: Is it possible and if yes, How to use custom package without github and without making it in the GO main folder rather by simply referencing the folder containing our go file from the directory we are working.问题:是否可能,如果是,如何在没有 github 的情况下使用自定义包,而不是在 GO 主文件夹中创建它,而不是简单地从我们正在工作的目录中引用包含我们的go文件的文件夹。

As the error shows, the compiler cannot find subpack from either如错误所示,编译器无法从任何一个中找到子subpack

/usr/local/go/src/subpack (from $GOROOT) /usr/local/go/src/subpack(来自 $GOROOT)

where the standard library packages (such as fmt , strings ) are, or标准库包(例如fmtstrings )所在的位置,或

/Users/anilbhatia/go/src/subpack (from $GOPATH) /Users/anilbhatia/go/src/subpack(来自 $GOPATH)

where user installed/defined packages are.用户安装/定义的包在哪里。

To make the import work, you just need to include the relative path of the subpack package (relative to $GOPATH/src ) in your main.go为了使进口的工作,你只需要包含的相对路径subpack包(相对于$GOPATH/src中的) main.go

Suppose your main.go is in /Users/anilbhatia/go/src/parentpack , then its import should be假设你的main.go/Users/anilbhatia/go/src/parentpack ,那么它的导入应该是

import "parentpack/subpack"

If I understand you correctly, you want the caller of subpack (say main.go ) to be in an unrelated location of subpack .如果我理解正确,您希望subpack的调用者(比如main.go )位于subpack的无关位置。 This actually works out of box.这实际上是开箱即用的。 Your main.go could be located anywhere.您的main.go可以位于任何地方。 When you compile it, the compiler sees the import path of parentpack/subpack , and goes to $GOPATH/src and $GOROOT/src to find it.编译的时候,编译器看到了parentpack/subpack的导入路径,会去$GOPATH/src$GOROOT/src找。

For more information about the source code organization and some typical examples, you can run有关源代码组织和一些典型示例的更多信息,您可以运行

 go help gopath

in your shell.在你的壳里。

The starting point is $GOPATH/src not your project's folder.起点是 $GOPATH/src 而不是您项目的文件夹。
So you should use所以你应该使用

import "myproject/subpack"

instead of :代替 :

import "subpack"

For some reason all the answers to this question are more than 2 years old and outdated.出于某种原因,这个问题的所有答案都已经超过 2 年并且已经过时了。 You can definitely do that now.你现在绝对可以做到。 I think there was a change in a recent version of Go that allows importing custom packages that are not in $GOPATH .我认为 Go 的最新版本发生了变化,允许导入不在$GOPATH自定义包。 You can do what you want to do simply by importing your "subpackage" like this in the main.go:你可以通过在 main.go 中像这样导入你的“子包”来做你想做的事情:

import "./subpack"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM