using libraries in Golang

huangapple go评论76阅读模式
英文:

using libraries in Golang

问题

首先,让我来翻译一下你的问题:

首先,我来自Java世界,最近一段时间一直在使用Go进行编程,我非常喜欢它。

我有一个关于包管理系统和导入的小问题,如果我导入一个使用另一个库的库,并且我已经在我的项目中使用了那个库,我如何消除重复(如果可能的话)?

换句话说:
A是主程序,C和B是库,然后:
C被添加到A中
B使用C

然后
B也被添加到A中

AProject/
    src/
        LibC/
            src/
                somefiles.go
        LibB/
            src/
                LibC
                somefiles.go
        app.go

现在我有两个C库,一个是一开始就在A中的,另一个是因为B依赖于C而添加到A中的。

我知道这有点令人困惑,但在Java世界中,我们有Ant和Maven这些构建工具,它们使我们处理依赖关系变得非常容易。

有什么想法吗?

对于你的问题,Go语言的包管理系统是通过导入路径来解决依赖关系的。当你导入一个库时,Go会自动解析并下载该库的依赖项。如果你已经在项目中使用了某个库,并且另一个库也依赖于同一个库,Go会自动处理重复的依赖项,确保只有一个实例被编译和使用。

在你的示例中,当你导入LibB时,它会自动解析并下载LibC作为其依赖项。由于LibC已经在A中作为直接依赖项存在,Go会自动处理重复的依赖项,确保只有一个LibC实例被编译和使用。

因此,你不需要手动处理重复的依赖项,Go会自动帮你处理。这是Go语言包管理系统的一个优点之一。

英文:

First of all and to be clear I come from the Java world, and I have been programming on go for a while and I really love it.

I have a small question about the packaging system and the imports, if I am importing a library which uses another library and I am already using that library in my project, how can I eliminate the duplication(if its possible),

in other words:
A is a the main program, C and B are libraries, and then:
C was added to A
B uses C

and then
B was also added to A

AProject/
    src/
        LibC/
            src/
                somefiles.go
        LibB/
            src/
                LibC
                somefiles.go
        app.go

So now I have two libraries of C one in A since the beginning and one in B because B is dependent on C.

I know its a little bit confusing but in the Java world we have Ant and Maven and those build tools make it really easy to us to handle the dependencies.

Any thoughts?

答案1

得分: 9

在Go语言中,不允许重复导入包。

首先,你应该阅读关于Go工作区的内容,可以参考如何编写Go代码

根据你的问题,你的目录结构应该类似于以下形式:

gopath(gopath是你$GOPATH列表中的一个目录路径)
├── bin
│   └── projecta
├── pkg
│   └── linux_amd64
│       └── projecta
│           ├── libb.a
│           └── libc.a
└── src
    └── projecta
        ├── a.go
        ├── libb
        │   └── b.go
        └── libc
            └── c.go

其中,

gopath/src/projecta/a.go:

package main

import (
    "projecta/libb"
    "projecta/libc"
)

func a() {
    libb.B()
    libc.C()
}

func main() { a() }

gopath/src/projecta/libb/b.go:

package libb

import (
    "projecta/libc"
)

func B() { libc.C() }

gopath/src/projecta/libc/c.go:

package libc

func C() {}
英文:

In Go, there is no duplication of packages.

First, you should read about Go workspaces in How to Write Go Code.

From your question, your directory structure should look something like this:

gopath (gopath is the path of a directory in your $GOPATH list)
├── bin
│   └── projecta
├── pkg
│   └── linux_amd64
│       └── projecta
│           ├── libb.a
│           └── libc.a
└── src
    └── projecta
        ├── a.go
        ├── libb
        │   └── b.go
        └── libc
            └── c.go

Where,

gopath/src/projecta/a.go:

package main

import (
	"projecta/libb"
	"projecta/libc"
)

func a() {
	libb.B()
	libc.C()
}

func main() { a() }

gopath/src/projecta/libb/b.go:

package libb

import (
	"projecta/libc"
)

func B() { libc.C() }

gopath/src/projecta/libc/c.go:

package libc

func C() {}

答案2

得分: 6

如果你在谈论第三方库,使用Go语言非常简单,只需在你的源代码中导入即可,例如:

import "github.com/somepackage/somelib"

然后在命令行中进入你的工作目录,运行:

go get

这样,库的源代码将会被下载到你的$GOPATH的src目录下。如果你想创建自己的库,只需在$GOPATH/src目录下创建以库名命名的文件夹,并将代码放入其中。

文件夹结构如下:

$GOPATH/
src/
    github.com/
        somepackage/
            somelib/
                somelib.go
    yourlibB/
        yourlibB.go -> // 在这里导入 somelib
    yourlibC/
        yourlibC.go -> // 在这里导入 libB
    yourmainprogramA/
        yourmainprogramA.go -> // 在这里导入 somelib、libC 和 libB

希望对你有所帮助!

英文:

If you are talking about third-party libraries, in go is very simple to do that,
just put the import in your source code, like:

import "github.com/somepackage/somelib"

and from the command line in your working directory run:

go get

the the source code of the libraries will be downloaded in the src directory of your $GOPATH.
If you want to create your own lib instead, just create the folder named as the lib in $GOPATH/src and put the code in this folder.

The folders structure is:

$GOPATH/
src/
    github.com/
        somepackage/
            somelib/
                somelib.go
    yourlibB/
        yourlibB.go -> //import somelib here
    yourlibC/
        yourlibC.go -> //import libB here
    yourmainprogramA/
        yourmainprogramA.go -> //import somelib, libC and libB here

huangapple
  • 本文由 发表于 2015年3月12日 17:33:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/29006070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定