Skip to content
Permalink
Browse files

Episode 27 - Adding a screencast on Go Modules (#171)

* Adding a screencast on Athens & Go Modules

Moving everything to episode 27 to accommodate new episodes

* adding gitignore under episode 27

* mods

* cats and dogs

* How to run this instructions

* adding a PS

* Adding screencast page

* YouTube ID
  • Loading branch information...
arschles committed Jul 16, 2019
1 parent c5287aa commit 28733d5163db53889c7a60a3b220dbe0b10ea1b5
@@ -48,5 +48,6 @@ episode25/tmp
episode25/tmp/episode25-build
episode25/node_modules
episode25/public/assets
episode25/hugo

episode26/ds
@@ -0,0 +1,5 @@
tmp
node_modules
public/assets
hugo
episode27
@@ -0,0 +1,52 @@
# Using Go Modules For Your Dependencies

Go in 5 Minutes, episode 27.

In this screencast, we're going to talk about [Go Modules](https://github.com/golang/go/wiki/Modules) to manage our dependencies.

Modules are a brand new dependency management system in Go, and they're here to stay. The modules technology is officially part of the `go` toolchain starting with version 1.11, and they're really nice. Modules have some features that are familiar to previous dependency management systems, like these:

- Define your dependencies in a file that you can check in with your code
- Keep track of all the dependencies -- including dependencies-of-your-dependencies (AKA transitive dependencies) -- in another file that you can check in with your code
- Keep checksums of all your dependencies (including transitive dependencies), so that things don't change out from under you

But they add some exciting new things too!

- Fetch all your dependencies without learning any other tools
- Delete your `vendor/` directory if you want
- Work outside the `GOPATH`
- Fetch your dependency code from somewhere _other than GitHub, GitLab, and other version control systems!_
- More on this in a future episode ...

We're going to show a little bit of how modules work and how you can use them in your code.

Check out the screencast for more!

# How To Build This Code

Because Go Modules is built into the `go` toolchain, building is pretty easy - you don't even need a Makefile or a `GOPATH` set up!

First, make sure you either don't have this `episode27` directory inside your `GOPATH`, _or_ that you have `GO111MODULE` set to `on` like this:

```console
$ export GO111MODULE=on
```

And then, you should be ready to go!

```console
$ go build -o heygophers .
$ ./heygophers
```

Once you run `./heygophers`, your web server should start up on port 8080, and you can open up the app at `http://localhost:8080` in your browser.

Enjoy!

(P.S. for an exciting look under the covers of what the Go Modules system is doing, clear your local modules cache with this command: `sudo rm -rf $(go env GOPATH)/pkg/mod`)

# Show Notes

- Just For Func _Intro to Go Modules and SemVer_: https://www.youtube.com/watch?v=aeF3l-zmPsY
- Just For Func _Migrating Go Modules to v2+_: https://www.youtube.com/watch?v=H_4eRD8aegk
- The Go modules wiki: https://github.com/golang/go/wiki/Modules
@@ -0,0 +1,5 @@
module github.com/arschles/go-in-5-minutes/episode27

go 1.12

require github.com/gin-gonic/gin v1.4.0
@@ -0,0 +1,28 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 h1:t8FVkw33L+wilf2QiWkw0UV77qRpcH/JHPKGpKa2E8g=
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
github.com/gin-gonic/gin v1.4.0 h1:3tMoCCfM7ppqsR0ptz/wi1impNpT7/9wQtMZ8lr1mCQ=
github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/3rZdM=
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc=
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/ugorji/go v1.1.4 h1:j4s+tAvLfL3bZyefP2SEWmhBzmuIlH/eqNuPdFPgngw=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ=
gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,31 @@
package main

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
)

func kittyHandler(c *gin.Context) {
cats := []string{
"/img/kitties/1.jpg",
"/img/kitties/2.jpg",
"/img/kitties/3.jpg",
"/img/kitties/4.jpg",
"/img/kitties/5.jpg",
"/img/kitties/6.jpg",
}
htmlStr := fmt.Sprintf(`<html>
<head></head>
<body>
<center>
<img src="%s" width="1000"/>
</center>
</body>
</html>`, randStr(cats))
c.Writer.Header().Set("Content-Type", "text/html")
c.Writer.WriteHeader(http.StatusOK)
c.Writer.WriteHeader(http.StatusOK)
c.Writer.Write([]byte(htmlStr))
}
@@ -0,0 +1,19 @@
package main

import (
"math/rand"
"time"

"github.com/gin-gonic/gin"
)

func main() {
rand.Seed(time.Now().UnixNano())
router := gin.Default()

router.Static("/img", "./img")
router.GET("/", mainHandler)
router.GET("/kitty", kittyHandler)
router.GET("/pup", pupHandler)
router.Run(":8080")
}
@@ -0,0 +1,24 @@
package main

import (
"net/http"

"github.com/gin-gonic/gin"
)

func mainHandler(c *gin.Context) {
htmlStr := `<html>
<head></head>
<body>
<center>
<h1>Hey Gophers!</h1>
<h3>What better way to demo Go Modules than with cat and dog pictures?</h3>
<p><a href="/kitty">Cats</a></p>
<p><a href="/pup">Dogs</a></p>
</center>
</body>
</html>`
c.Writer.Header().Set("Content-Type", "text/html")
c.Writer.WriteHeader(http.StatusOK)
c.Writer.Write([]byte(htmlStr))
}
@@ -0,0 +1,33 @@
package main

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
)

func pupHandler(c *gin.Context) {
dogs := []string{
"/img/pups/1.jpg",
"/img/pups/2.jpg",
"/img/pups/3.png",
"/img/pups/3.png",
"/img/pups/4.png",
"/img/pups/5.png",
"/img/pups/6.jpg",
"/img/pups/7.jpg",
}

htmlStr := fmt.Sprintf(`<html>
<head></head>
<body>
<center>
<img src="%s" width="1000"/>
</center>
</body>
</html>`, randStr(dogs))
c.Writer.Header().Set("Content-Type", "text/html")
c.Writer.WriteHeader(http.StatusOK)
c.Writer.Write([]byte(htmlStr))
}
@@ -0,0 +1,15 @@
package main

import (
"math/rand"
"time"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

func randStr(slc []string) string {
idx := rand.Intn(len(slc))
return slc[idx]
}
@@ -0,0 +1,24 @@
+++
type = "screencast"
title = "Using Go Modules"
teaser = "Go Modules is the future of dependency management in Go. We should all learn it, so get started here!"
author = "Aaron Schlesinger"
date = "2019-07-16T13:28:20-07:00"
+++

_Episode 27_

Go Modules is a brand new system, but it's _the way_ we are going to do dependency management in Go, so we all need to be familiar with it!

<!--more-->

Go Modules was introduced in Go version 1.11, and it's going to be the default way to pull in and publish dependencies in Go. While you can still use other dependency management tools, modules are the future, so let's get onboard and learn all about them!

This episode gives you a taste of how they work and how to use them (hint: it's simpler than you think!). After you watch this one, you'll be able to go get started converting your codebase over to modules, or just experimenting with it if you like. Either way, I hope you enjoy!

Keep on rockin', Gophers!

<iframe width="560" height="315" src="https://www.youtube.com/embed/71hgdExuCbg" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>

Check out the example code [on Github](https://github.com/arschles/go-in-5-minutes/tree/master/episode27).

0 comments on commit 28733d5

Please sign in to comment.
You can’t perform that action at this time.