Skip to content

Instantly share code, notes, and snippets.

@wanabe
Last active December 8, 2019 11:32
Show Gist options
  • Select an option

  • Save wanabe/29645b4bb6089a45b6e737cff12d066d to your computer and use it in GitHub Desktop.

Select an option

Save wanabe/29645b4bb6089a45b6e737cff12d066d to your computer and use it in GitHub Desktop.

この文章は

マイクロサービスを全然知らないので実験してみた作業記録です。

構想

gRPC で rejoiner 使ったら叩くときも楽かなー、やったことないから go やってみたいなー、くらいのことしか考えてません。 題材はなんでもいいんですが、git の GUI クライアントがほしいのでその想定で。

記録

.proto

まずはこれがないとはじまらないインターフェース定義。いろいろ足したくなるけどとりあえず最初はこんなもんで。

gitm.proto
syntax = "proto3";
package pb;

message Object {
    string hash = 1;
}

message Commit {
    Object object = 1;
    Object parent = 2;
}

名前に特に意味はありません。git の protoc しようとしたところでまったく何も用意してなかったのでコマンドがないと怒られました。のでインストールしてから実行。

$ sudo snap install --classic protobuf
$ protoc --go_out=pb gitm.proto

シリアライズ

通信する前に、go の書き方や出力ファイルの使い方を見るためにまずはシリアライズだけしてみます。

gitm.go
package main
import (
    "fmt"
    "encoding/hex"

    "github.com/golang/protobuf/proto"

    "./pb"
)

func main() {
    commit := pb.Commit {
        Object: &pb.Object {
            Hash: "4df003e28a16e91e9667c7e6ea5852202820ac67",
        },
    }
    out, err := proto.Marshal(&commit)
    if err != nil {
        panic(err)
    }

    fmt.Printf("%s\ndone.\n", hex.Dump(out))
}

出力を見てみると、

$ make && ./gitm 
protoc --go_out=pb gitm.proto
go build
00000000  0a 2a 0a 28 34 64 66 30  30 33 65 32 38 61 31 36  |.*.(4df003e28a16|
00000010  65 39 31 65 39 36 36 37  63 37 65 36 65 61 35 38  |e91e9667c7e6ea58|
00000020  35 32 32 30 32 38 32 30  61 63 36 37              |52202820ac67|

done.

よさそうです。 (gRPC と protobuf と混同していたり & をつけ忘れていたりもろもろ試行錯誤した部分はカットしています)

あとから go.mod を足したりそのために "./pb""gitm/pb" にしたりしたものがこちら

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment