jsonパッケージを使う
1 |
import "encoding/json" |
変換する
jsonパッケージの引数に配列を指定してあげれば変換してくれる
1 |
<span class="n">data</span><span class="p">,</span> <span class="n">_</span> <span class="o">:=</span> <span class="n">json</span><span class="o">.</span><span class="n">Marshal</span><span class="p">(</span><span class="n">m</span><span class="p">)</span> |
例えばKubernetesのPod一覧をjsonに変換するもの
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<span class="k">package</span> <span class="n">main</span> <span class="k">import</span> <span class="p">(</span> <span class="s">"context"</span> <span class="s">"encoding/json"</span> <span class="s">"fmt"</span> <span class="n">metav1</span> <span class="s">"k8s.io/apimachinery/pkg/apis/meta/v1"</span> <span class="s">"k8s.io/client-go/kubernetes"</span> <span class="s">"k8s.io/client-go/tools/clientcmd"</span> <span class="s">"log"</span> <span class="s">"os"</span> <span class="s">"path/filepath"</span> <span class="p">)</span> <span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> <span class="c">// Kubeconfigのファイルパスを指定</span> <span class="n">kubeconfig</span> <span class="o">:=</span> <span class="n">filepath</span><span class="o">.</span><span class="n">Join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">Getenv</span><span class="p">(</span><span class="s">"HOME"</span><span class="p">),</span> <span class="s">".kube"</span><span class="p">,</span> <span class="s">"config"</span><span class="p">)</span> <span class="n">config</span><span class="p">,</span> <span class="n">err</span> <span class="o">:=</span> <span class="n">clientcmd</span><span class="o">.</span><span class="n">BuildConfigFromFlags</span><span class="p">(</span><span class="s">""</span><span class="p">,</span> <span class="n">kubeconfig</span><span class="p">)</span> <span class="k">if</span> <span class="n">err</span> <span class="o">!=</span> <span class="no">nil</span> <span class="p">{</span> <span class="n">log</span><span class="o">.</span><span class="n">Fatal</span><span class="p">(</span><span class="n">err</span><span class="p">)</span> <span class="p">}</span> <span class="c">// Kubeconfigを読み込む</span> <span class="n">clientset</span><span class="p">,</span> <span class="n">err</span> <span class="o">:=</span> <span class="n">kubernetes</span><span class="o">.</span><span class="n">NewForConfig</span><span class="p">(</span><span class="n">config</span><span class="p">)</span> <span class="k">if</span> <span class="n">err</span> <span class="o">!=</span> <span class="no">nil</span> <span class="p">{</span> <span class="n">log</span><span class="o">.</span><span class="n">Fatal</span><span class="p">(</span><span class="n">err</span><span class="p">)</span> <span class="p">}</span> <span class="c">// Pod一覧を呼び出す</span> <span class="n">namespace</span> <span class="o">:=</span> <span class="s">"default"</span> <span class="n">pods</span><span class="p">,</span> <span class="n">err</span> <span class="o">:=</span> <span class="n">clientset</span><span class="o">.</span><span class="n">CoreV1</span><span class="p">()</span><span class="o">.</span><span class="n">Pods</span><span class="p">(</span><span class="n">namespace</span><span class="p">)</span><span class="o">.</span><span class="n">List</span><span class="p">(</span><span class="n">context</span><span class="o">.</span><span class="n">TODO</span><span class="p">(),</span> <span class="n">metav1</span><span class="o">.</span><span class="n">ListOptions</span><span class="p">{})</span> <span class="k">if</span> <span class="n">err</span> <span class="o">!=</span> <span class="no">nil</span> <span class="p">{</span> <span class="n">log</span><span class="o">.</span><span class="n">Fatalln</span><span class="p">(</span><span class="s">"failed to get pods:"</span><span class="p">,</span> <span class="n">err</span><span class="p">)</span> <span class="p">}</span> <span class="n">m</span> <span class="o">:=</span> <span class="k">map</span><span class="p">[</span><span class="kt">string</span><span class="p">]</span><span class="kt">string</span><span class="p">{}</span> <span class="c">// print pods</span> <span class="c">// pods.Items: []v1.Pod</span> <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">pod</span> <span class="o">:=</span> <span class="k">range</span> <span class="n">pods</span><span class="o">.</span><span class="n">Items</span> <span class="p">{</span> <span class="n">m</span><span class="p">[</span><span class="n">pod</span><span class="o">.</span><span class="n">Name</span><span class="p">]</span> <span class="o">=</span> <span class="kt">string</span><span class="p">(</span><span class="n">pod</span><span class="o">.</span><span class="n">Status</span><span class="o">.</span><span class="n">Phase</span><span class="p">)</span> <span class="p">}</span> <span class="n">data</span><span class="p">,</span> <span class="n">_</span> <span class="o">:=</span> <span class="n">json</span><span class="o">.</span><span class="n">Marshal</span><span class="p">(</span><span class="n">m</span><span class="p">)</span> <span class="n">fmt</span><span class="o">.</span><span class="n">Printf</span><span class="p">(</span><span class="kt">string</span><span class="p">(</span><span class="n">data</span><span class="p">))</span> <span class="p">}</span> |
実行結果
1 2 3 4 5 6 7 |
<span class="p">{</span> <span class="nl">"external-dns-547f4784f7-852zf"</span><span class="p">:</span> <span class="s2">"Running"</span><span class="p">,</span> <span class="nl">"nginx-deployment-574b87c764-8vwdz"</span><span class="p">:</span> <span class="s2">"Running"</span><span class="p">,</span> <span class="nl">"nginx-deployment-574b87c764-dnr6m"</span><span class="p">:</span> <span class="s2">"Running"</span><span class="p">,</span> <span class="nl">"v01-app-54fc4b5d44-k5zfp"</span><span class="p">:</span> <span class="s2">"Running"</span><span class="p">,</span> <span class="nl">"v01-db-5b756dc6b-57grv"</span><span class="p">:</span> <span class="s2">"Running"</span> <span class="p">}</span> |
marshalとunmarshal
marshalは元帥?という意味?黒ひげ?
marshal
- structをjsonに変換するときに使う
unmarshal
ネットワーク越しに受け取ったjsonのbyte配列をjsonへ変換するときに使う
- たぶんレスポンスヘッダーとかそのあたりをうまいこと取り除いてくれるんだと思う