Learning Golang: Dependencies, Modules and How to manage Packages



Learning Golang: Dependencies, Modules and How to manage Packages

Learning Golang: Dependencies, Modules and How to manage Packages

Welcome! Let’s learn about Go Modules!

What is a “Go Module”? A module is a collection of Go packages stored in a file tree with a “go.mod” file at its root. This file defines:

* Module path: indicates how our package will be imported by our users, and
* Dependency Requirements, indicate the packages we depend on.

“Dependency Requirements” not only covers the actual packages but also ways to “Replace” or “Exclude” them, using the corresponding directives:

* Replace: allows replacing a version of a dependency with another one, for example in cases where local deployment is still happening or when working for forked versions; and
* Exclude: allows excluding a version of a dependency from use, for example in cases those versions include security issues or bugs.

“Go Modules” were introduced in Go 1.11 and enabled by default in Go 1.16, they use “Semantic Versioning” (SemVer) as the main versioning system, were versions are defined using three numbers: Major.Minor.Patch.

The Go toolchain allows interacting with Modules via the “mod” command, but not only that other commands like “get” or “build” are also compatible with Go Modules, the most used commands are:

* “go mod init” to initialize a module, for example “go mod init github.com/MarioCarrion/example-pkg”
* “go get _package/version_@_specific-version_” go get packages with specific version, for example “go get github.com/jackc/pgx/[email protected]
* “go get” can be used to upgrade or downgrade as well, for example to downgrade: “go get github.com/jackc/pgx/[email protected]” or update to latest “go get -u github.com/jackc/pgx/v4”
* “go mod tidy” to remove unused dependencies.

* “The Go Blog: Using Go Modules” https://blog.golang.org/using-go-modules

Real life examples of versioning Go Modules:

* Using module path: https://github.com/googleapis/gax-go
* Using branches: https://github.com/olivere/elastic

Tools:

* https://github.com/oligot/go-mod-upgrade: Update outdated Go dependencies interactively
* https://github.com/marwan-at-work/mod: Automated Semantic Import Versioning Upgrades

* Previous Episode: “Golang Tutorial: Context package: Cancellations, Deadlines and Request-scoped values”: https://youtu.be/mgJMIZsWfB4
* Blog: https://mariocarrion.com/2021/06/07/learning-golang-dependencies-modules.html

* Playlist “Learning Golang”: https://www.youtube.com/playlist?list=PL7yAAGMOat_F7bOImcjx4ZnCtfyNEqzCy
* Playlist “Building Microservices in Go/Golang”: https://www.youtube.com/playlist?list=PL7yAAGMOat_Fn8sAXIk0WyBfK_sT1pohu
* Playlist “Golang Tools and Packages”: https://www.youtube.com/playlist?list=PL7yAAGMOat_HEEOvH99agDs_5g51A0Ls3
* Playlist “Testing in Go”: https://www.youtube.com/playlist?list=PL7yAAGMOat_HSeW4zF0uRL9EaHadE4ZZq

00:00 – Start
00:08 – What is a Go Module?
01:09 – go.mod? require? replace? exclude?
02:12 – go.mod in real life
02:36 – go.mod indirect?
03:06 – go.sum
03:58 – Versioning Go Modules: Path or Branch
05:29 – Demo, go mod init
08:59 – Module upgrade and downgrade
13:18 – Requesting available updates
14:19 – Versioning: Real life examples
15:26 – Go Modules: Conclusion and parting words

Who am I:

HelloπŸ‘‹πŸΌ! I’m Mario, a Hands-on Software Architect and Lead Backend Engineer with more than 16 years of professional experience building all kinds of software including on-premise Industrial Automation Systems, Linux Accessibility Desktop and Browser Components as well as Distributed Advertising Microservices.

Every week I will share with you different topics I’ve learned while working for small startups and large companies including the processes I’ve followed over the years for successfully delivering complex enterprise systems from start to end.

Subscribe if you like Software Development, Software Architecture and Systems Design!

Keep it up. Don’t give up!

#golang #tutorial #programming

— Our affiliate links below

Shop our favorite products β†’ https://www.amazon.com/shop/rubycarrion
Vlogging setup β†’ https://rubycarrion.com/camerasforvlogging/
Get a 30 day FREE trial of Epidemic Sound β†’ https://www.epidemicsound.com/referral/szx441/
Try Amazon Prime 30-Day FREE trial β†’ https://amzn.to/3s0el1R
I love getting Cash Back and think you will too! Join for free and get $20 when you spend $20 β†’ https://www.rakuten.com/r/RUBYRA132?eeid=28187
Sign up to start using vidIQ to grow your YouTube channel for FREE β†’ https://vidiq.com/rubycarrion

— Our Vlog Channel

https://www.youtube.com/c/RubyCarrion/videos

Comments are closed.