ポンコツエンジニアのごじゃっぺ開発日記。

いろいろポンコツだけど、気にするな。エンジニアの日々の開発などの記録を残していきます。 自動で収入を得られるサービスやシステムを作ることが目標!!

go言語のVersioningであるvgoを使ってみる

mac上でgo言語の開発をしているのですが、バージョン管理機能であるvgoを使ってみたいと思います。

導入時のメモをこちらに書いていきたいと思います。

vgoのインストール

goがインストールされた状態で、以下のコマンドを入力する。go言語のインストールについてはこちらの記事にまとめてあります。はじめてのGo言語をmac上で触ってみた。

$ go get -u golang.org/x/vgo

導入はこれだけ。そうvgoのドキュメントにも書いてありました。

じゃあ実際にコマンドを叩いてみよう!

$ vgo
-bash: vgo: command not found

あれ?vgoコマンドが見つからない。。と思って、いろいろ調べてみました。

$ ls ~/go/bin/
vgo

$ cd ~/go/bin/
$ ./vgo
Go is a tool for managing Go source code.

This is vgo, an experimental go command with support for package versioning.
Even though you are invoking it as vgo, most of the messages printed will
still say "go", not "vgo". Sorry.

Usage:

go command [arguments]

The commands are:

bug start a bug report
build compile packages and dependencies
clean remove object files and cached files
doc show documentation for package or symbol
env print Go environment information
fix update packages to use new APIs
fmt gofmt (reformat) package sources
generate generate Go files by processing source
get download and install packages and dependencies
install compile and install packages and dependencies
list list packages
run compile and run Go program
test test packages
tool run specified go tool
vendor vendor dependencies of current module
verify verify downloaded modules against expected hashes
version print Go version
vet report likely mistakes in packages

Use "go help [command]" for more information about a command.

Additional help topics:

buildmode build modes
c calling between Go and C
cache build and test caching
environment environment variables
filetype file types
gopath GOPATH environment variable
importpath import path syntax
packages package lists
testflag testing flags
testfunc testing functions

Use "go help [topic]" for more information about that topic.

おお!どうやらパスが通ってないみたい!

ということなので、以下を~/.bash_profileに追加しました。

export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin

で、もう一度実行!

$ vgo
Go is a tool for managing Go source code.

This is vgo, an experimental go command with support for package versioning.
Even though you are invoking it as vgo, most of the messages printed will
still say "go", not "vgo". Sorry.

Usage:

go command [arguments]

The commands are:

bug start a bug report
build compile packages and dependencies
clean remove object files and cached files
doc show documentation for package or symbol
env print Go environment information
fix update packages to use new APIs
fmt gofmt (reformat) package sources
generate generate Go files by processing source
get download and install packages and dependencies
install compile and install packages and dependencies
list list packages
run compile and run Go program
test test packages
tool run specified go tool
vendor vendor dependencies of current module
verify verify downloaded modules against expected hashes
version print Go version
vet report likely mistakes in packages

Use "go help [command]" for more information about a command.

Additional help topics:

buildmode build modes
c calling between Go and C
cache build and test caching
environment environment variables
filetype file types
gopath GOPATH environment variable
importpath import path syntax
packages package lists
testflag testing flags
testfunc testing functions

Use "go help [topic]" for more information about that topic.


$ vgo version
go version go1.10.1 darwin/amd64 vgo:2018-02-20.1

無事vgoの導入が完了しました!

 

実際に使ってみる

ではさっそく使ってみたいと思います。

ソースはA Tour of Versioned Go(vgo)に書いてあるものをそのまま使ってみます。まずはそのまま使うのがいいよね。

$ vim hello.go

hello.go

package main // import "github.com/you/hello"

import (
  "fmt"
  "rsc.io/quote"
)

func main() {
  fmt.Println(quote.Hello())
}
$ echo > go.mod

$ vgo build
vgo: resolving import "rsc.io/quote"
vgo: finding rsc.io/quote (latest)
vgo: adding rsc.io/quote v1.5.2
vgo: finding rsc.io/quote v1.5.2
vgo: finding rsc.io/sampler v1.3.0
vgo: finding golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
vgo: downloading rsc.io/quote v1.5.2
vgo: downloading rsc.io/sampler v1.3.0
vgo: downloading golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c

$ ./hello
こんにちは世界。

実行できました!ちなみにgo.modの中身は以下のように書き換えられています。

$ cat go.mod
module "github.com/you/hello"

require "rsc.io/quote" v1.5.2

ここにどんどん追加されていくんですかね。

もちろんそのままhello.goを実行しようとしてもエラーになってしまいます。

$ go run hello.go
hello.go:5:2: cannot find package "rsc.io/quote" in any of:
/usr/local/Cellar/go/1.10.1/libexec/src/rsc.io/quote (from $GOROOT)
/Users/*******/go/src/rsc.io/quote (from $GOPATH)

 

最後に

という感じで、mac上でvgoを使うことができました。

さあ、これでいい感じの開発ができそうですね。

お問い合わせプライバシーポリシー制作物