设为首页 加入收藏

TOP

golang 标准库间依赖的可视化展示(一)
2017-10-09 08:55:35 】 浏览:2340
Tags:golang 标准 依赖 可视化 展示

简介

国庆看完 << Go 语言圣经 >>,总想做点什么,来加深下印象.以可视化的方式展示 golang 标准库之间的依赖,可能是一个比较好的切入点.做之前,简单搜了下相关的内容,网上也要讨论,但是没有发现直接能拿过来用的.标准库之间,是必然存在依赖关系的,不同库被依赖的程度必然是不一样的.但究竟有多大差别呢?

以下内容,数据源自真实环境的 golang 1.9 版本的标准库.所以,本文不仅是一篇可视化相关的讨论文章,更是提供了一个可以直接探究 golang 标准库间依赖关系的快速梳理工具.

数据准备

标准库各个包之间的相互关系,可以直接通过命令获取,然后简单变换为一个标准的 JSON 对象:

go list -json  std

示例输出:

{
    "Dir": "/usr/local/go/src/archive/tar",
    "ImportPath": "archive/tar",
    "Name": "tar",
    "Doc": "Package tar implements access to tar archives.",
    "Target": "/usr/local/go/pkg/darwin_amd64/archive/tar.a",
    "Goroot": true,
    "Standard": true,
    "StaleReason": "standard package in Go release distribution",
    "Root": "/usr/local/go",
    "GoFiles": [
        "common.go",
        "format.go",
        "reader.go",
        "stat_atimespec.go",
        "stat_unix.go",
        "strconv.go",
        "writer.go"
    ],
    "IgnoredGoFiles": [
        "stat_atim.go"
    ],
    "Imports": [
        "bytes",
        "errors",
        "fmt",
        "io",
        "io/ioutil",
        "math",
        "os",
        "path",
        "sort",
        "strconv",
        "strings",
        "syscall",
        "time"
    ],
    "Deps": [
        "bytes",
        "errors",
        "fmt",
        "internal/cpu",
        "internal/poll",
        "internal/race",
        "io",
        "io/ioutil",
        "math",
        "os",
        "path",
        "path/filepath",
        "reflect",
        "runtime",
        "runtime/internal/atomic",
        "runtime/internal/sys",
        "sort",
        "strconv",
        "strings",
        "sync",
        "sync/atomic",
        "syscall",
        "time",
        "unicode",
        "unicode/utf8",
        "unsafe"
    ],
    "TestGoFiles": [
        "reader_test.go",
        "strconv_test.go",
        "tar_test.go",
        "writer_test.go"
    ],
    "TestImports": [
        "bytes",
        "crypto/md5",
        "fmt",
        "internal/testenv",
        "io",
        "io/ioutil",
        "math",
        "os",
        "path",
        "path/filepath",
        "reflect",
        "sort",
        "strings",
        "testing",
        "testing/iotest",
        "time"
    ],
    "XTestGoFiles": [
        "example_test.go"
    ],
    "XTestImports": [
        "archive/tar",
        "bytes",
        "fmt",
        "io",
        "log",
        "os"
    ]
}

梳理过的数据源,参见: https://raw.githubusercontent.com/ios122/graph-go/master/data.js

可视化原理

主要涉及一下内容:

  • 可视化显示,使用的是 echarts

  • 使用原始数据的 ImportPath 而不是 Name,来作为每个数据节点的唯一id.这样是因为 golang 本身的包命名规范决定的.

  • 使用原始数据的 Imports 字段,来确定标准库包与包之间的相互依赖关系.golang是不允许循环依赖的,所以一些循环依赖相关的问题,不需要考虑.

  • 节点的大小,和包被其他包引入的次数成正相关.这样做,被依赖越多的包,图上最终显示时,就会越大.常用包和不常用包,一目了然.

数据整理

就是把原始数据,处理成 echarts 需要的数据,这里简要说下最核心的思路:

  • echarts 显示相关的代码,很大程度上参考了 graph-npm

  • 节点坐标和

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Go语言备忘录:反射的原理与使用.. 下一篇go语言之并发

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目