48

Taking Go modules for a spin

 5 years ago
source link: https://www.tuicool.com/articles/hit/JZZBJzv
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

This weekend I wanted to play with Ubuntu 18.04 on a spare machine. This gave me a perfect excuse to try out the modules feature recently merged into the Go 1.11 development branch.

TL;DR: When Go 1.11 ships you’ll be able to download the tarball and unpack it anywhere you like. When Go 1.11 ships you’ll be able to write Go code anywhere you like. 

Setup

This is the tricky bit. Go 1.11 beta 1 shipped back in June so it predates the inclusion of the modules feature. Hopefully there will be a beta 2 in a few weeks and when that ships I’ll delete this section, but for now, I’ll quickly outline the steps to build Go 1.11 from source .

First, we need a version of Go to act as the bootstrap compiler, I’m using Go 1.10, which I’ll download to $HOME/go1.10 .

% <b>curl https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz | \
        tar tvz --transform=s/go/go.10/g</b>
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  126M  100  126M    0     0  1140k      0  0:01:53  0:01:53 --:--:-- 1093k
% <b>go1.10/bin/go</b>
go version go1.10.3 linux/amd64

Next, I’ll build Go 1.11 from the master branch using ./make.bash .

% <b>git clone https://go.googlesource.com/go go.tip -b master</b>
Cloning into 'go.tip'...
remote: Sending approximately 132.25 MiB ...
remote: Counting objects: 30, done
remote: Finding sources: 100% (8/8)
remote: Total 349279 (delta 284434), reused 349276 (delta 284434)
Receiving objects: 100% (349279/349279), 132.19 MiB | 1.42 MiB/s, done.
Resolving deltas: 100% (284434/284434), done.
Checking out files: 100% (7977/7977), done.
% <b>cd go.tip/src</b>
% <b>env GOROOT_BOOTSTRAP=$HOME/go1.10 ./make.bash</b>
Building Go cmd/dist using /home/dfc/go1.10.
Building Go toolchain1 using /home/dfc/go1.10.
Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1.
Building Go toolchain2 using go_bootstrap and Go toolchain1.
Building Go toolchain3 using go_bootstrap and Go toolchain2.
Building packages and commands for linux/amd64.
---
Installed Go for linux/amd64 in /home/dfc/go.tip
Installed commands in /home/dfc/go.tip/bin

Lastly, out of shot, I’ve added $HOME/go.tip/bin to my $PATH .

Kicking the tires

Now we have a version of Go with module support installed, I wanted to try to use it to manage the dependencies for httpstat , a clone of the Python tool of the same name that many collaborators swarmed on to build in late 2016.

To show that Go 1.11 won’t need you to declare a $GOPATH or use a specific directly layout for the location of your project, I’m going to use my favourite directory for source code, ~/devel .

% <b>git clone https://github.com/davecheney/httpstat devel/httpstat</b>
Cloning into 'devel/httpstat'...
remote: Counting objects: 2326, done.
remote: Total 2326 (delta 0), reused 0 (delta 0), pack-reused 2326
Receiving objects: 100% (2326/2326), 8.73 MiB | 830.00 KiB/s, done.
Resolving deltas: 100% (673/673), done.
Checking out files: 100% (1361/1361), done.
% <b>cd devel/httpstat</b>
% <b>go mod -init -module github.com/davecheney/httpstat</b>
go: creating new go.mod: module github.com/davecheney/httpstat
go: copying requirements from Gopkg.lock

Nice, go mod -init translated my existing Gopkg.lock file into its own go.mod format.

% <b>cat go.mod</b>
module github.com/davecheney/httpstat

require (
        github.com/fatih/color v1.5.0
        github.com/mattn/go-colorable v0.0.9
        github.com/mattn/go-isatty v0.0.3
        golang.org/x/net v0.0.0-20170922011244-0744d001aa84
        golang.org/x/sys v0.0.0-20170922123423-429f518978ab
        golang.org/x/text v0.0.0-20170915090833-1cbadb444a80
)

Let’s give it a try

% <b>go build</b> 
go: finding golang.org/x/net v0.0.0-20170922011244-0744d001aa84
go: finding github.com/mattn/go-colorable v0.0.9
go: finding github.com/mattn/go-isatty v0.0.3
go: finding golang.org/x/sys v0.0.0-20170922123423-429f518978ab
go: finding github.com/fatih/color v1.5.0
go: finding golang.org/x/text v0.0.0-20170915090833-1cbadb444a80
go: downloading github.com/fatih/color v1.5.0
go: downloading github.com/mattn/go-isatty v0.0.3
go: downloading golang.org/x/net v0.0.0-20170922011244-0744d001aa84
go: downloading github.com/mattn/go-colorable v0.0.9
go: downloading golang.org/x/text v0.0.0-20170915090833-1cbadb444a80

Very nice, go build ignored the vendor/ folder in this repository (because we’re outside $GOPATH ) and fetched the revisions it needed. Let’s try out the binary and make sure it works.

% <b>./httpstat golang.org</b>

Connected to 216.58.196.145:443

HTTP/2.0 200 OK
Server: Google Frontend
Alt-Svc: quic=":443"; ma=2592000; v="44,43,39,35"
Cache-Control: private
Content-Type: text/html; charset=utf-8
Date: Sat, 14 Jul 2018 08:20:43 GMT
Strict-Transport-Security: max-age=31536000; preload
Vary: Accept-Encoding
X-Cloud-Trace-Context: 323cd59570cc084fed506f7e85d79d9f

Body discarded

  DNS Lookup   TCP Connection   TLS Handshake   Server Processing   Content Transfer
[    171ms  |          18ms  |        559ms  |            226ms  |             5ms  ]
            |                |               |                   |                  |
   namelookup:171ms          |               |                   |                  |
                       connect:189ms         |                   |                  |
                                   pretransfer:749ms             |                  |
                                                     starttransfer:976ms            |
                                                                                total:981ms

Yup, nothing to see here.

Wrap up

You can build Go 1.11 from source right now anywhere you like. You don’t need to set an environment variable or follow a prescribed location.

With Go 1.11 and modules you can write your code anywhere you like . You’re no longer forced into having one copy of a project checked out in a specific sub directory of your $GOPATH .

  1. There’s no need to run the ./all.bash tests, the build dashboard and trybots have done that for me.
  2. Oddly these are stored in $HOME/go/src/mod not the $GOCACHE variable that was added in Go 1.10.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK