doc.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. Package semver provides the ability to work with Semantic Versions (http://semver.org) in Go.
  3. Specifically it provides the ability to:
  4. * Parse semantic versions
  5. * Sort semantic versions
  6. * Check if a semantic version fits within a set of constraints
  7. * Optionally work with a `v` prefix
  8. Parsing Semantic Versions
  9. To parse a semantic version use the `NewVersion` function. For example,
  10. v, err := semver.NewVersion("1.2.3-beta.1+build345")
  11. If there is an error the version wasn't parseable. The version object has methods
  12. to get the parts of the version, compare it to other versions, convert the
  13. version back into a string, and get the original string. For more details
  14. please see the documentation at https://godoc.org/github.com/Masterminds/semver.
  15. Sorting Semantic Versions
  16. A set of versions can be sorted using the `sort` package from the standard library.
  17. For example,
  18. raw := []string{"1.2.3", "1.0", "1.3", "2", "0.4.2",}
  19. vs := make([]*semver.Version, len(raw))
  20. for i, r := range raw {
  21. v, err := semver.NewVersion(r)
  22. if err != nil {
  23. t.Errorf("Error parsing version: %s", err)
  24. }
  25. vs[i] = v
  26. }
  27. sort.Sort(semver.Collection(vs))
  28. Checking Version Constraints
  29. Checking a version against version constraints is one of the most featureful
  30. parts of the package.
  31. c, err := semver.NewConstraint(">= 1.2.3")
  32. if err != nil {
  33. // Handle constraint not being parseable.
  34. }
  35. v, err := semver.NewVersion("1.3")
  36. if err != nil {
  37. // Handle version not being parseable.
  38. }
  39. // Check if the version meets the constraints. The a variable will be true.
  40. a := c.Check(v)
  41. Basic Comparisons
  42. There are two elements to the comparisons. First, a comparison string is a list
  43. of comma separated and comparisons. These are then separated by || separated or
  44. comparisons. For example, `">= 1.2, < 3.0.0 || >= 4.2.3"` is looking for a
  45. comparison that's greater than or equal to 1.2 and less than 3.0.0 or is
  46. greater than or equal to 4.2.3.
  47. The basic comparisons are:
  48. * `=`: equal (aliased to no operator)
  49. * `!=`: not equal
  50. * `>`: greater than
  51. * `<`: less than
  52. * `>=`: greater than or equal to
  53. * `<=`: less than or equal to
  54. Hyphen Range Comparisons
  55. There are multiple methods to handle ranges and the first is hyphens ranges.
  56. These look like:
  57. * `1.2 - 1.4.5` which is equivalent to `>= 1.2, <= 1.4.5`
  58. * `2.3.4 - 4.5` which is equivalent to `>= 2.3.4, <= 4.5`
  59. Wildcards In Comparisons
  60. The `x`, `X`, and `*` characters can be used as a wildcard character. This works
  61. for all comparison operators. When used on the `=` operator it falls
  62. back to the pack level comparison (see tilde below). For example,
  63. * `1.2.x` is equivalent to `>= 1.2.0, < 1.3.0`
  64. * `>= 1.2.x` is equivalent to `>= 1.2.0`
  65. * `<= 2.x` is equivalent to `<= 3`
  66. * `*` is equivalent to `>= 0.0.0`
  67. Tilde Range Comparisons (Patch)
  68. The tilde (`~`) comparison operator is for patch level ranges when a minor
  69. version is specified and major level changes when the minor number is missing.
  70. For example,
  71. * `~1.2.3` is equivalent to `>= 1.2.3, < 1.3.0`
  72. * `~1` is equivalent to `>= 1, < 2`
  73. * `~2.3` is equivalent to `>= 2.3, < 2.4`
  74. * `~1.2.x` is equivalent to `>= 1.2.0, < 1.3.0`
  75. * `~1.x` is equivalent to `>= 1, < 2`
  76. Caret Range Comparisons (Major)
  77. The caret (`^`) comparison operator is for major level changes. This is useful
  78. when comparisons of API versions as a major change is API breaking. For example,
  79. * `^1.2.3` is equivalent to `>= 1.2.3, < 2.0.0`
  80. * `^1.2.x` is equivalent to `>= 1.2.0, < 2.0.0`
  81. * `^2.3` is equivalent to `>= 2.3, < 3`
  82. * `^2.x` is equivalent to `>= 2.0.0, < 3`
  83. */
  84. package semver