20200107のGoに関する記事は4件です。

Goの内部パッケージ関係をコミットごとに可視化する

call-aimation.gif

使用したプログラム: bxcodec/go-clean-arch

概要

  1. Goの内部パッケージの関係が複雑なプログラムは理解するのが大変
  2. 可視化したい
  3. せっかくだからコミットごとに可視化してアニメーションにしてみる

準備

コード

やったこと

  1. コミットログを取得
  2. コミットごとにコマンドを実行
  3. Goの内部パッケージ関係をdotファリルに出力し、gifを作成
  4. コミットごとのgifのサイズを揃えて、アニメーションを作成

1. コミットログを取得

git log --pretty=oneline で取得できる。

pythonでかくと

res = subprocess.check_output(["git", "-C", path_dir, "log", "--pretty=oneline"])

コミットのハッシュ値だけを取得したい(ハッシュ値+コメントになっている)。空白で区切って一番最初のハッシュ値だけを取得してリストにした。

2. コミットごとにコマンド実行

コミットごとにresetしてコマンドを実行した(reset以外にいい方法がある??)。

for commit in log_list:
    subprocess.call(["git", "-C", path_dir, "reset", "--hard", commit]) |
    command(path_dir)

コマンドを実行するときはプログラムのあるディレクトリのパスを使用する。

3. Goの内部パッケージ関係をdotファリルに出力し、gifを作成

3.1. dotファイル作成

ここが一番難しい。
parser というとても便利なパッケージがあり、ファイル内でimportしているパッケージを取得してくれる。これをファイルごとからパッケージごとにまとめれば良い。

困ったこと

  • ファイルごと --> パッケージごと
    parserはファイルのパッケージ名も取得してくれるが、別のディレクトリに同じパッケージ名があると区別できない。ディレクトリを保存したが、もっと簡単な方法がありそう
  • 外部パッケージの区別
    内部パッケージ(自分で作ったもの)だけを対象にしたかったがどうやって区別すればいいか??「githubってあれば外部」みたいにしたが、あまりいい方法ではなさそう。

3.2. dotファイルからgifファイル作成

graphvizを使用する。
dot -T gif sample.dot -o sample.gif

4. コミットごとのgifのサイズを揃えて、アニメーションを作成

4.1. gifのサイズを揃える

アニメーションにするときファイルごとのサイズが違うと大きいサイズは切れてしまう。
mogrify -resize (width)x(height)! *.gif
指定するサイズはいい感じのを選ぶ。

4.2. アニメーション作成

ImageMagickを使用する。
convert -delay 5 -loop 0 *.gif animation.gif

参考にしたサイト

これから

コミットごとにグラフの表示位置がバラバラになってしまいアニメーションにしたとき見にくい。どうにか揃えたい。

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

goでSTUNサーバを使ってパブリックIPアドレスを取得

以下の記事を参考に Go の実装に置き換えてみた。

以下のパッケージ使って同等のことが割と簡単に実装できた。

Go言語楽しい。

実行結果

# go run webrtc/main.go
2020/01/06 23:31:08 Local IP Address: 192.168.xxx.xxx
2020/01/06 23:31:08 Public IP Address: 113.xxx.xxx.xxx

実装

main.go
package main

import (
  "log"
  "os"

  "github.com/pion/webrtc/v2"
)

func main() {
  config := webrtc.Configuration{
    ICEServers: []webrtc.ICEServer{
      {
        URLs: []string{"stun:stun.l.google.com:19302"},
      },
    },
  }

  // generate a new connection
  peerConnection, err := webrtc.NewPeerConnection(config)
  if err != nil {
    log.Fatal(err)
  }

  peerConnection.OnICECandidate(func(c *webrtc.ICECandidate) {
    if c == nil {
      os.Exit(0)
    }
    switch c.Typ {
    case webrtc.ICECandidateTypeHost:
      log.Println("Local IP Address:", c.Address)
    case webrtc.ICECandidateTypeSrflx:
      log.Println("Public IP Address:", c.Address)
    }
  })

  if _, err := peerConnection.CreateDataChannel("", nil); err != nil {
    log.Fatal(err)
  }

  offer, err := peerConnection.CreateOffer(nil)
  if err != nil {
    log.Fatal(err)
  }

  if err = peerConnection.SetLocalDescription(offer); err != nil {
    log.Fatal(err)
  }

  // block forever
  select {}
}
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

GoでSTUNサーバを使ってパブリックIPアドレスを取得

以下の記事を参考に Go の実装に置き換えてみた。

以下のパッケージ使って同等のことが割と簡単に実装できた。

Go言語楽しい。

実行結果

# go run webrtc/main.go
2020/01/06 23:31:08 Local IP Address: 192.168.xxx.xxx
2020/01/06 23:31:08 Public IP Address: 113.xxx.xxx.xxx

実装

main.go
package main

import (
  "log"
  "os"

  "github.com/pion/webrtc/v2"
)

func main() {
  config := webrtc.Configuration{
    ICEServers: []webrtc.ICEServer{
      {
        URLs: []string{"stun:stun.l.google.com:19302"},
      },
    },
  }

  // generate a new connection
  peerConnection, err := webrtc.NewPeerConnection(config)
  if err != nil {
    log.Fatal(err)
  }

  peerConnection.OnICECandidate(func(c *webrtc.ICECandidate) {
    if c == nil {
      os.Exit(0)
    }
    switch c.Typ {
    case webrtc.ICECandidateTypeHost:
      log.Println("Local IP Address:", c.Address)
    case webrtc.ICECandidateTypeSrflx:
      log.Println("Public IP Address:", c.Address)
    }
  })

  if _, err := peerConnection.CreateDataChannel("", nil); err != nil {
    log.Fatal(err)
  }

  offer, err := peerConnection.CreateOffer(nil)
  if err != nil {
    log.Fatal(err)
  }

  if err = peerConnection.SetLocalDescription(offer); err != nil {
    log.Fatal(err)
  }

  // block forever
  select {}
}
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

GoでSTUNサーバを使って自分のパブリックIPアドレスを取得

以下の記事を参考に Go の実装に置き換えてみた。

以下のパッケージ使って同等のことが割と簡単に実装できた。

Go言語楽しい。

実行結果

# go run webrtc/main.go
2020/01/06 23:31:08 Local IP Address: 192.168.xxx.xxx
2020/01/06 23:31:08 Public IP Address: 113.xxx.xxx.xxx

実装

main.go
package main

import (
  "log"
  "os"

  "github.com/pion/webrtc/v2"
)

func main() {
  config := webrtc.Configuration{
    ICEServers: []webrtc.ICEServer{
      {
        URLs: []string{"stun:stun.l.google.com:19302"},
      },
    },
  }

  // generate a new connection
  peerConnection, err := webrtc.NewPeerConnection(config)
  if err != nil {
    log.Fatal(err)
  }

  peerConnection.OnICECandidate(func(c *webrtc.ICECandidate) {
    if c == nil {
      os.Exit(0)
    }
    switch c.Typ {
    case webrtc.ICECandidateTypeHost:
      log.Println("Local IP Address:", c.Address)
    case webrtc.ICECandidateTypeSrflx:
      log.Println("Public IP Address:", c.Address)
    }
  })

  if _, err := peerConnection.CreateDataChannel("", nil); err != nil {
    log.Fatal(err)
  }

  offer, err := peerConnection.CreateOffer(nil)
  if err != nil {
    log.Fatal(err)
  }

  if err = peerConnection.SetLocalDescription(offer); err != nil {
    log.Fatal(err)
  }

  // block forever
  select {}
}
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む