Guest User

Untitled

a guest
Oct 20th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. ## Creating, Upgrading, and Consuming Modules
  2.  
  3. In this "starter" example, we will demonstrate several important attributes of Go modules, including how modules:
  4.  
  5. * provide semantic versioning for a collection of related Go packages
  6. * record precise dependencies
  7. * provide for 100% reproducible builds
  8. * allow upgrades for backwards compatable changes as well as breaking changes
  9.  
  10. ## Example Overview
  11.  
  12. At different points in the walkthrough, we will adopt one of two different roles:
  13.  
  14. * **Module Publisher** – creates a `starterlib` module from scratch and releases several versions
  15. * **Module Consumer** – imports and uses the `starterlib` module in a separate `starterconsumer` module
  16.  
  17. We pretend that time moves foward as the **Module Publisher**:
  18. 1. creates a v1.0.0 of `starterlib`
  19. 2. introduces new backwards compatible functionality in v1.1.0 of `starterlib`
  20. 3. introduces breaking changes in v2.0.0 of `starterlib`
  21.  
  22. At each step, the **Module Consumer** upgrades `starterconsumer` to use the new version of `starterlib`.
  23.  
  24. ## Walkthrough
  25.  
  26. ### Module Publisher: Create v1.0.0 of starterlib
  27.  
  28. First, **Module Publisher** creates a simple package that prints hello along
  29. with a Go proverb in a `WiseHello` function.
  30.  
  31. setup starterlib:
  32.  
  33. ```
  34. {{PrintBlock "setup starterlib" -}}
  35. ```
  36.  
  37. define starterlib repo module:
  38.  
  39. ```
  40. {{PrintBlock "define starterlib repo module" -}}
  41. ```
  42.  
  43. write v1.0.0 code:
  44.  
  45. ```
  46. {{PrintBlock "write v1.0.0 code" -}}
  47. ```
  48.  
  49. build v1.0.0 code:
  50.  
  51. ```
  52. {{PrintBlock "build v1.0.0 code" -}}
  53. ```
  54.  
  55. init git:
  56.  
  57. ```
  58. {{PrintBlock "init git" -}}
  59. ```
  60.  
  61. tag v1.0.0:
  62.  
  63. ```
  64. {{PrintBlock "tag v1.0.0" -}}
  65. ```
  66.  
  67. ### Module Consumer: Import and use v1.0.0 of starterlib
  68.  
  69. Now we switch roles to the **Module Consumer**, and create a separate
  70. simple module that imports `starterlib`.
  71.  
  72. setup starterconsumer:
  73.  
  74. ```
  75. {{PrintBlock "setup starterconsumer" -}}
  76. ```
  77.  
  78. define starterconsumer repo module:
  79.  
  80. ```
  81. {{PrintBlock "define starterconsumer repo module" -}}
  82. ```
  83.  
  84. write v1.0.0 consumer:
  85.  
  86. ```
  87. {{PrintBlock "write v1.0.0 consumer" -}}
  88. ```
  89.  
  90. build and run v1.0.0 consumer:
  91.  
  92. ```
  93. {{PrintBlock "build and run v1.0.0 consumer" -}}
  94. ```
  95.  
  96. cat v1.0.0 consumer go.mod:
  97.  
  98. ```
  99. {{PrintBlock "cat v1.0.0 consumer go.mod" -}}
  100. ```
  101.  
  102. review v1.0.0 consumer versions:
  103.  
  104. ```
  105. {{PrintBlock "review v1.0.0 consumer versions" -}}
  106. ```
  107.  
  108. ### Module Publisher: Create v1.1.0 of starterlib
  109.  
  110. Now the **Module Publisher** introduces new backwards compatible functionality in v1.1.0
  111. of `starterlib`.
  112.  
  113. To make the example slightly more interesting, we create also introduce
  114. a new `hello` package in `starterlib`, and refactor our initial `WiseHello` functionality
  115. to use the new package while maintaining the exact behavior for `WiseHello` that we
  116. had in v1.0.0 to maintain backwards compatibility.
  117.  
  118. add new package for v1.1.0 code:
  119.  
  120. ```
  121. {{PrintBlock "add new package for v1.1.0 code" -}}
  122. ```
  123.  
  124. review v1.1.0 code organization:
  125.  
  126. ```
  127. {{PrintBlock "review v1.1.0 code organization" -}}
  128. ```
  129.  
  130. refactor wise.go for v1.1.0 code:
  131.  
  132. ```
  133. {{PrintBlock "refactor wise.go for v1.1.0 code" -}}
  134. ```
  135.  
  136. build v1.1.0 code:
  137.  
  138. ```
  139. {{PrintBlock "build v1.1.0 code" -}}
  140. ```
  141.  
  142. commit changes:
  143.  
  144. ```
  145. {{PrintBlock "commit changes" -}}
  146. ```
  147.  
  148. tag v1.1.0:
  149.  
  150. ```
  151. {{PrintBlock "tag v1.1.0" -}}
  152. ```
  153.  
  154. ### Module Consumer: Import and use v1.1.0 of starterlib
  155.  
  156. Now we switch roles back to the **Module Consumer** again, and
  157. upgrade to use v1.1.0 of `starterlib` in the `starterconsumer` module.
  158.  
  159. rebuild consumer after publishing v1.1.0:
  160.  
  161. ```
  162. {{PrintBlock "rebuild consumer after publishing v1.1.0" -}}
  163. ```
  164.  
  165. cat consumer go.mod after publishing v1.1.0:
  166.  
  167. ```
  168. {{PrintBlock "cat consumer go.mod after publishing v1.1.0" -}}
  169. ```
  170.  
  171. review consumer upgrade versions after publishing v1.1.0:
  172.  
  173. ```
  174. {{PrintBlock "review consumer upgrade versions after publishing v1.1.0" -}}
  175. ```
  176.  
  177. upgrade to v1.1.0:
  178.  
  179. ```
  180. {{PrintBlock "upgrade to v1.1.0" -}}
  181. ```
  182.  
  183. rebuild consumer after upgrading to v1.1.0:
  184.  
  185. ```
  186. {{PrintBlock "rebuild consumer after upgrading to v1.1.0" -}}
  187. ```
  188.  
  189. review consumer upgrade versions after upgrading to v1.1.0:
  190.  
  191. ```
  192. {{PrintBlock "review consumer upgrade versions after upgrading to v1.1.0" -}}
  193. ```
  194.  
  195. ### Module Publisher: Create v2.0.0 of starterlib
  196.  
  197. Now the **Module Publisher** breaks compatability by introducing new backwards _incompatible_
  198. changes to `starterlib`
  199.  
  200. This triggers the need to:
  201. * increment the major release number
  202. * add `/v2` to the module path in the `module` directive in the `go.mod` for `starterlib`
  203. * add `/v2` to the import paths used internally for `starterlib`
  204. * release as v2.0.0
  205.  
  206. refactor wise.go for v2.0.0 code:
  207.  
  208. ```
  209. {{PrintBlock "refactor wise.go for v2.0.0 code" -}}
  210. ```
  211.  
  212. update go.mod for v2.0.0 code:
  213.  
  214. ```
  215. {{PrintBlock "update go.mod for v2.0.0 code" -}}
  216. ```
  217.  
  218. build v2.0.0 code:
  219.  
  220. ```
  221. {{PrintBlock "build v2.0.0 code" -}}
  222. ```
  223.  
  224. commit v2.0.0 changes:
  225.  
  226. ```
  227. {{PrintBlock "commit v2.0.0 changes" -}}
  228. ```
  229.  
  230. tag v2.0.0:
  231.  
  232. ```
  233. {{PrintBlock "tag v2.0.0" -}}
  234. ```
  235.  
  236. ### Module Consumer: Import and use v2.0.0 of starterlib
  237.  
  238. Now we switch roles back to the **Module Consumer** one last time, and
  239. upgrade to use v2.0.0 of `starterlib` in the `starterconsumer` module.
  240.  
  241. To do this, we need to:
  242. * add `/v2` to the module path in the `require` directive for `starterlib` in the `starterconsumer` `go.mod`
  243. * add `/v2` to the import path for `starterlib` in our `starterconsumer` code.
  244.  
  245. review consumer versions after publishing v2.0.0:
  246.  
  247. ```
  248. {{PrintBlock "review consumer versions after publishing v2.0.0" -}}
  249. ```
  250.  
  251. rebuild consumer after upgrading to v2.0.0:
  252.  
  253. ```
  254. {{PrintBlock "rebuild consumer after upgrading to v2.0.0" -}}
  255. ```
  256.  
  257. version details:
  258.  
  259. ```
  260. {{PrintBlock "version details" -}}
  261. ```
Add Comment
Please, Sign In to add comment