Go / golang – 它有类似于 Python 的 “pip install” 来安装包的等效方法吗?

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

Go / golang - does it have equivalent of python "pip install" to install packages?

问题

刚刚开始学习Go(人们是说"Go"还是"Golang"?)

我成功运行了"Hello World"的例子。我已经设置好了我的GOROOT和GOPATH。

现在我想做一些更高级的事情,比如打开CSV文件,我在这里找到了一个教程来做这个链接

为了使这个脚本工作,我需要导入的包,比如"bufio","encoding/csv"等等。

我需要手动搜索https://github.com/golang/go/wiki/Projects或其他仓库,然后下载并解压到我的GOPATH的"pkg"目录中吗?

还是Go/Golang有类似Python的"pip install"的东西可以为我完成这个操作?

import (
    "bufio"
    "encoding/csv"
    "os"
    "fmt"
    "io"
)

func main() {
    // 加载一个TXT文件。
    f, _ := os.Open("C:\\Users\\bb\\Documents\\Dropbox\\Data\\bc hydro tweets\\bchtweets.csv")

    // 创建一个新的读取器。
    r := csv.NewReader(bufio.NewReader(f))
    for {
        record, err := r.Read()
        // 在EOF处停止。
        if err == io.EOF {
            break
        }
        // 显示记录。
        // ... 显示记录的长度。
        // ... 显示切片的所有单独元素。
        fmt.Println(record)
        fmt.Println(len(record))
        for value := range record {
            fmt.Printf("  %v\n", record[value])
        }
    }
}
英文:

Have just started learning about Go (do people say "Go" or "Golang"?)

I got the hello world example running. I have my GOROOT AND GOPATH set up.

Now I want to do something bit more advanced, for example open csv file, for which I found a tutorial to do that here

In order to make this script work, I need the packages that are being imported eg "bufio", "encoding/csv", etc.

Do I have to manually search https://github.com/golang/go/wiki/Projects or some other repository, download and unzip these into my GOPATH "pkg" directory?

Or does Go/Golang have something equivalent to Python's "pip install" that would do this for me?

import (
    "bufio"
    "encoding/csv"
    "os"
    "fmt"
    "io"
)

func main() {
    // Load a TXT file.
    f, _ := os.Open("C:\\Users\\bb\\Documents\\Dropbox\\Data\\bc hydro tweets\\bchtweets.csv")

    // Create a new reader.
    r := csv.NewReader(bufio.NewReader(f))
    for {
    record, err := r.Read()
    // Stop at EOF.
    if err == io.EOF {
        break
    }
    // Display record.
    // ... Display record length.
    // ... Display all individual elements of the slice.
    fmt.Println(record)
    fmt.Println(len(record))
    for value := range record {
        fmt.Printf("  %v\n", record[value])
    }
    }
}

答案1

得分: 11

Go语言有一个类似于Python中的pip install的命令go get。(参考)

更新:从Go 1.17开始,使用go get安装可执行文件已被弃用。可以使用go install代替。(参考)

英文:

Go has go get which is similar to pip install in Python. (ref)

UPDATE: Starting in Go 1.17, installing executables with go get is deprecated. go install may be used instead.(ref)

答案2

得分: 5

请阅读这个美丽文档部分的每一行内容:https://golang.org/doc/code.html#Organization

顺便说一下,你在导入部分使用的所有包都来自标准库。所以你不需要为这个示例安装任何东西。

安装包的最佳方式是使用 go get 命令,它会将一个 git 仓库克隆到你的 $GOPATH/src 目录下,你应该尽可能地使用它。
如果你必须使用某个包的特定版本,你可以为指定的提交创建一个分支,然后使用 go get 命令获取该分支,或者使用众多的包管理工具之一 https://github.com/golang/go/wiki/PackageManagementTools

英文:

Read every single line of this beautiful documentation's section: https://golang.org/doc/code.html#Organization

BTW, all packages you have in your import section are from standard library. So you don't have to install anything for this example.

Best way to install a package is go get which simply clones a git repo to your $GOPATH/src and you should stick to it as long as you can.
If you must use some package version you can create fork for a specified commit and go get that fork or use one of many vendoring toolds https://github.com/golang/go/wiki/PackageManagementTools

答案3

得分: 4

Glide软件包管理器可能是与pip最接近的选项。它有配置文件(和锁定文件),可以指定版本等。

在Go 1.6发布之前,您需要设置环境变量GO15VENDOREXPERIMENT=1,以便go工具可以在vendor/文件夹中找到软件包。在Go 1.6中,这将默认启用。Glide将软件包存储在vendor/文件夹中,而不是GOPATH(尽管根项目需要在GOPATH中),以便不同的应用程序可以拥有并定期使用不同版本的依赖项。

如果您想要一些略有不同的东西,可以在维基页面上列出的众多软件包管理器中选择。

声明:我是Glide的开发人员之一。Pip是它的灵感之一。

英文:

The Glide package manager is maybe your closest option to pip. You have config (and lock) files, can specify versions, etc.

Until Go 1.6 is out you'll need to set the environment variable GO15VENDOREXPERIMENT=1 for the go tool to pickup the packages in the vendor/ folder. In Go 1.6 this will be on by default. Glide stores packages in a vendor/ folder instead of the GOPATH (even though the root project needs to be in the GOPATH) so that different applications can have and regularly use different versions of dependencies.

If you want something a little different there are numerous package managers listed on the wiki.

Disclosure: I'm on of Glide's developers. Pip was one of the inspirations for it.

huangapple
  • 本文由 发表于 2016年1月4日 04:40:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/34581352.html
匿名

发表评论

匿名网友

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

确定