development

go get을 사용하여 특정 버전의 패키지를 가져 오려면 어떻게해야합니까?

big-blog 2020. 9. 16. 08:06
반응형

go get을 사용하여 특정 버전의 패키지를 가져 오려면 어떻게해야합니까?


Node특정 버전의 공급 업체 lib를 프로젝트 폴더 ( node_modules)에 설치하는 데 사용 했던 환경 에서 다음과 같이 npm해당 lib의 해당 버전을 package.json콘솔에서 또는 콘솔에서 직접 설치하도록 지시합니다 .

$ npm install express@4.0.0

그런 다음 프로젝트에서 해당 패키지의 해당 버전을 다음과 같이 가져 왔습니다.

var express = require('express');

이제 go. 어떻게 할 수 있습니까? 특정 버전의 패키지를 설치할 수 있습니까? 그렇다면 중앙 집중식을 사용하여 $GOPATH다른 버전 대신 한 버전을 가져올 수 있습니까?

나는 다음과 같이 할 것입니다.

$ go get github.com/wilk/uuid@0.0.1
$ go get github.com/wilk/uuid@0.0.2

하지만 가져 오는 동안 어떻게 차이를 만들 수 있습니까?


업데이트 18-11-23 : Go 1.11 모드는 공식 실험입니다. @krish 답변을 참조하십시오.
19-01-01 업데이트 : From Go 1.12 mod는 여전히 공식 실험입니다. Go 1.13부터는 모듈 모드가 모든 개발의 기본값이됩니다.

이전 답변 :

당신은 offical 한으로 버전을 설정할 수 있습니다 출발

dep ensure --add github.com/gorilla/websocket@1.2.0

아무도 gopkg.in 을 언급하지 않았다는 사실에 놀랐습니다 .

gopkg.in실제로 저장소를 만들지 않고도 버전을 저장소 URL로 표현할 수있는 래퍼 (리디렉션)를 제공하는 서비스입니다. gopkg.in/yaml.v1gopkg.in/yaml.v2, 심지어 모두 라이브 그들은 비록https://github.com/go-yaml/yaml

저자가 적절한 버전 관리 관행을 따르지 않는 경우 (이전 버전과의 호환성을 깨뜨릴 때 버전 번호를 늘림) 완벽하지 않지만 브랜치 및 태그와 함께 작동합니다.


Go 1.11에는 go 모듈이라는 기능이 있으며 버전과 함께 종속성을 간단히 추가 할 수 있습니다.

단계

  1. go mod init .
  2. go mod edit -require github.com/wilk/uuid@0.0.1
  3. 가서 -v -t. / ...
  4. 짓다
  5. 가서 설치

해당 주제에 대한 자세한 정보 : https://github.com/golang/go/wiki/Modules


을 사용 git checkout하여 특정 버전을 얻고이 버전을 사용하여 프로그램을 빌드 할 수 있습니다.

예:

export GOPATH=~/
go get github.com/whateveruser/whateverrepo
cd ~/src/github.com/whateveruser/whateverrepo
git tag -l
# supose tag v0.0.2 is correct version
git checkout tags/v0.0.2
go run whateverpackage/main.go

Glide 는 특히 Node의 npm 또는 Rust의화물에서 온 경우 Go를위한 정말 우아한 패키지 관리입니다.

1.6에서 Godep의 새로운 공급 업체 기능과 밀접하게 작동하지만 훨씬 더 쉽습니다. 종속성 및 버전은 GOPATH에 의존하지 않고 projectdir / vendor 디렉터리 내에 "잠 깁니다".

Brew로 설치 (OS X)

$ brew install glide

glide.yaml 파일을 초기화합니다 (package.json과 유사). 또한 GOPATH에서 프로젝트의 기존 가져온 패키지를 가져 와서 프로젝트의 vendor / 디렉토리에 복사합니다.

$ glide init

새 패키지 받기

$ glide get vcs/namespace/package

패키지 버전을 업데이트하고 잠급니다. 그러면 프로젝트 디렉토리에 glide.lock 파일이 생성되어 버전을 잠급니다.

$ glide up

글라이드를 시도했고 현재 프로젝트에서 즐겁게 사용하고 있습니다.


Go 1.5에는 종속성을 관리하는 데 도움이 되는 "공급 업체 실험" 이 있습니다. Go 1.6부터는 더 이상 실험이 아닙니다. Go wiki에는 다른 옵션있습니다. .

Edit: as mentioned in this answer gopkg.in is a good option for pinning github-depdencies pre-1.5.


dep is the official experiment for dependency management for Go language. It requires Go 1.8 or newer to compile.

To start managing dependencies using dep, run the following command from your project's root directory:

dep init

After execution two files will be generated: Gopkg.toml ("manifest"), Gopkg.lock and necessary packages will be downloaded into vendor directory.

Let's assume that you have the project which uses github.com/gorilla/websocket package. dep will generate following files:

Gopkg.toml

# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
#   name = "github.com/user/project"
#   version = "1.0.0"
#
# [[constraint]]
#   name = "github.com/user/project2"
#   branch = "dev"
#   source = "github.com/myfork/project2"
#
# [[override]]
#  name = "github.com/x/y"
#  version = "2.4.0"


[[constraint]]
  name = "github.com/gorilla/websocket"
  version = "1.2.0"

Gopkg.lock

# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.


[[projects]]
  name = "github.com/gorilla/websocket"
  packages = ["."]
  revision = "ea4d1f681babbce9545c9c5f3d5194a789c89f5b"
  version = "v1.2.0"

[solve-meta]
  analyzer-name = "dep"
  analyzer-version = 1
  inputs-digest = "941e8dbe52e16e8a7dff4068b7ba53ae69a5748b29fbf2bcb5df3a063ac52261"
  solver-name = "gps-cdcl"
  solver-version = 1

There are commands which help you to update/delete/etc packages, please find more info on official github repo of dep (dependency management tool for Go).


go get is the Go package manager. It works in a completely decentralized way and how package discovery still possible without a central package hosting repository.

Besides locating and downloading packages, the other big role of a package manager is handling multiple versions of the same package. Go takes the most minimal and pragmatic approach of any package manager. There is no such thing as multiple versions of a Go package.

go get always pulls from the HEAD of the default branch in the repository. Always. This has two important implications:

  1. As a package author, you must adhere to the stable HEAD philosophy. Your default branch must always be the stable, released version of your package. You must do work in feature branches and only merge when ready to release.

  2. New major versions of your package must have their own repository. Put simply, each major version of your package (following semantic versioning) would have its own repository and thus its own import path.

    e.g. github.com/jpoehls/gophermail-v1 and github.com/jpoehls/gophermail-v2.

As someone building an application in Go, the above philosophy really doesn't have a downside. Every import path is a stable API. There are no version numbers to worry about. Awesome!

For more details : http://zduck.com/2014/go-and-package-versioning/


The approach I've found workable is git's submodule system. Using that you can submodule in a given version of the code and upgrading/downgrading is explicit and recorded - never haphazard.

The folder structure I've taken with this is:

+ myproject
++ src
+++ myproject
+++ github.com
++++ submoduled_project of some kind.

Nowadays you can just use go get for it. You can fetch your dependency by the version tag, branch or even the commit.

go get github.com/someone/some_module@master
go get github.com/someone/some_module@v1.1.0
go get github.com/someone/some_module@commit_hash

more details here - How to point Go module dependency in go.mod to a latest commit in a repo?

Go get will also install the binary, like it says in the documentation -

Get downloads the packages named by the import paths, along with their dependencies. It then installs the named packages, like 'go install'.

(from https://golang.org/cmd/go/)


There's a go edit -replace command to append a specific commit (even from another forked repository) on top of the current version of a package. What's cool about this option, is that you don't need to know the exact pseudo version beforehand, just the commit hash id.

For example, I'm using the stable version of package "github.com/onsi/ginkgo v1.8.0".

Now I want - without modifying this line of required package in go.mod - to append a patch from my fork, on top of the ginkgo version:

$ GO111MODULE="on"  go mod edit -replace=github.com/onsi/ginkgo=github.com/manosnoam/ginkgo@d6423c2

After the first time you build or test your module, GO will try to pull the new version, and then generate the "replace" line with the correct pseudo version. For example in my case, it will add on the bottom of go.mod:

replace github.com/onsi/ginkgo => github.com/manosnoam/ginkgo v0.0.0-20190902135631-1995eead7451


That worked for me

GO111MODULE=on go get -u github.com/segmentio/aws-okta@v0.22.1

참고URL : https://stackoverflow.com/questions/24855081/how-do-i-import-a-specific-version-of-a-package-using-go-get

반응형