Skip to content

Instantly share code, notes, and snippets.

@djui
Last active February 17, 2016 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djui/824922442c123a08b71f to your computer and use it in GitHub Desktop.
Save djui/824922442c123a08b71f to your computer and use it in GitHub Desktop.
Print a list of all most-outer scope Go functions sorted by their length.
#!/usr/bin/env python3
import glob
import json
import sys
def main(args):
if not args:
print("Usage: go-funclen BASEDIR", file=sys.stderr)
return 1
basedir = args[0]
files = glob.iglob(basedir + '/*/*.go')
res = []
for f in files:
i = 0
marker_n = 0
marker_sig = ''
for line in open(f).readlines():
i += 1
if line.startswith('func '):
marker_n = i
marker_sig = line.strip()[5:-1]
if line.startswith('}'):
if marker_n > 0:
res.append((f, marker_sig, i-marker_n-1))
marker_n = -1
marker_sig = ''
for e in sorted(res, key=lambda x: x[2]):
print('{: >3} {: <35} {}'.format(e[2], e[0], e[1]))
if __name__ == '__main__':
try:
sys.exit(main(sys.argv[1:]))
except KeyboardInterrupt:
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment