Skip to content

Instantly share code, notes, and snippets.

@mattdee123
Created October 6, 2021 21:38
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 mattdee123/b23068d4933c4a2b09d25518b6bac18f to your computer and use it in GitHub Desktop.
Save mattdee123/b23068d4933c4a2b09d25518b6bac18f to your computer and use it in GitHub Desktop.
gen_module.py
import os
MAX_DEPTH = 11
def gen_package(name, levels):
subpkgs = [name+'a', name+'b']
if levels == 1:
subpkgs = []
for subpkg in subpkgs:
gen_package(subpkg, levels-1)
imports = '\n'.join(f'\t"example.com/{x}"' for x in subpkgs)
expr = '1'
if subpkgs:
expr = ' + '.join(f'{x}.V' for x in subpkgs)
os.mkdir(name)
with open(name + '/pkg.go', 'w') as f:
f.write(f'''package {name}
import (
{imports}
)
const V = {expr}
''')
return name
gen_package('pkg_', MAX_DEPTH)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment