Gopkg: a convenient go pakcage Management Method

Source: Internet
Author: User

In the process of using go, we sometimes introduce some third-party libraries for use. The common method is to usego getBut this method has a very serious problem. If a third-party library updates related interfaces, you may not be able to use it, so we have a good package management mechanism.

In my article on best practices of the Go language in the production environment, I introduced soundcloud's practices and checked the code of the third library directly and put it in the vender directory of my project, or use godep.

But now I have found a better way to manage gopkg. It uses a URL with a version number to allow go tool to check the specified version library. Although it only supports go Repositories on GitHub, I think it is powerful enough.

A simple example is to obtain the yaml package of go using the following method:

go get gopkg.in/yaml.v1

In fact, the address of the yaml package is:

https://github.com/go-yaml/yaml

Yaml. V1 indicates that the version is V1, and on GitHub, there is a corresponding V1 branch.

The URL format supported by gopkg is simple:

gopkg.in/pkg.v3      → github.com/go-pkg/pkg (branch/tag v3, v3.N, or v3.N.M)gopkg.in/user/pkg.v3 → github.com/user/pkg   (branch/tag v3, v3.N, or v3.N.M)

We use V. N to define a version, and then create a branch with the same name on GitHub. Gopkg support(vMAJOR[.MINOR[.PATCH]])For this type of version mode, if multiple versions with the same major, such as V1, v1.0.1, and v1.1.2, gopkg uses the highest level v1.1.2, such as the following versions:

  • V1
  • V2.0
  • V2.0.3
  • V2.1.2
  • V3
  • V3.0

The gopkg method is as follows:

  • PKG. V1-> V1
  • PKG. v2-> v2.1.2
  • PKG. V3-> V3.0

V0, or version 0, is not recommended for gopkg.

Gopkg also lists some suggestions. Do you need to upgrade the main version or not after the code is updated? In some cases, you must upgrade the main version:

  • Deletes or renames any export APIs, functions, and variables.
  • Add, delete, or rename a function to an interface
  • Add parameters to functions or interfaces
  • Change the parameter or return value type of a function or interface
  • Change the number of returned values of a function or interface
  • Change struct

In this case, you do not need to upgrade the main version:

  • Added the export interface, function, or variable
  • Rename the parameter name of the function or interface.
  • Change struct

The above mentioned changes to the struct. For example, if I add a field to a struct, you may not need to upgrade the primary version. However, if you delete an export field of the struct, you must upgrade it. If you simply change the non-exported fields in the structure, you do not need to upgrade it.

For more details, see gopkg

As you can see, gopkg uses a very simple way for us to conveniently manage go pakcage versions. So I made a V1 version for my log package. You can directlygo get gopkg.in/siddontang/go-log.v1/log.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.