Cloud Source Repositoriesにおいたリポジトリをgo getする

2019/01/01

Categories: golang gcp

※ この話はgo1.11.4と2019/01/01JST時点のCloud Source Repositoriesを前提としています。

プライベートなリポジトリをGCPのCloud Source Repositoriesに置いている。

GCPプロジェクトがmypj-xxxxxでリポジトリ名がcheckならリモートリポジトリのURLは https://source.developers.google.com/p/mypj-xxxxx/r/check という感じ。

下記で説明されているようにgcloudを使った認証か.gitcookieを用いることでgitコマンドを使ってアクセスできるようになる。
リポジトリのクローン作成

で、このリポジトリをGo Modules利用でgo getしようとすると

# 必要に応じてexport GO111MODULE=on
$ go get source.developers.google.com/p/mypj-xxxxx/r/check
go get source.developers.google.com/p/mypj-xxxxx/r/check: unrecognized import path "source.developers.google.com/p/mypj-xxxxx/r/check" (parse https://source.developers.google.com/p/mypj-xxxxx/r/check?go-get=1: no go-import meta tags ())

という感じでエラーになってしまう。

go getが指定されたURLのVCSを特定するためのmeta情報を得られないのが理由らしい。

<!-- githubの例 -->
<meta name="go-import" content="github.com/tckz/redmine-wiki_graphviz_plugin git https://github.com/tckz/redmine-wiki_graphviz_plugin.git">

URLにVCS(git)を示す修飾子を付けることでgo getできるようになる。

$ go get source.developers.google.com/p/mypj-xxxxx/r/check.git

go getされる側

// go.mod  
module source.developers.google.com/p/mypj-xxxxx/r/check.git
// check.go
package check

import "fmt"

func MyFunc() {
    fmt.Println("MyFunc")
}
// some/func.go
package some

import "fmt"

func CoolFunc() {
    fmt.Println("Cool")
}

go getする側

// go.mod
module source.developers.google.com/p/mypj-xxxxx/r/sample.git
  
require source.developers.google.com/p/mypj-xxxxx/r/check.git v0.1.0
// main.go
package main

import (
    "source.developers.google.com/p/mypj-xxxxx/r/check.git"
    "source.developers.google.com/p/mypj-xxxxx/r/check.git/some"
)

func main() {
    check.MyFunc()
    some.CoolFunc()
}

環境

>> Home