Skip to content

Instantly share code, notes, and snippets.

@unstppbl
Created February 3, 2025 11:35
Show Gist options
  • Save unstppbl/694fde2517f7fd7f78d35d69ec592e4b to your computer and use it in GitHub Desktop.
Save unstppbl/694fde2517f7fd7f78d35d69ec592e4b to your computer and use it in GitHub Desktop.
python script to combine all .go files of the project into one file
import os
def merge_files_in_folder(folder_path, output_file):
"""
Merges all .go files in the specified folder and its subfolders into a single file, excluding the vendor directory.
Args:
folder_path (str): Path to the folder containing .go files to merge.
output_file (str): Path to the output file where content will be merged.
"""
with open(output_file, 'w', encoding='utf-8') as outfile:
for root, _, files in os.walk(folder_path):
if 'vendor' in root.split(os.sep):
continue
for file in files:
if file.endswith('.go'):
file_path = os.path.join(root, file)
try:
with open(file_path, 'r', encoding='utf-8') as infile:
outfile.write(f"\n--- Start of {file_path} ---\n")
outfile.write(infile.read())
outfile.write(f"\n--- End of {file_path} ---\n")
except Exception as e:
print(f"Could not read {file_path}: {e}")
if __name__ == "__main__":
folder_path = input("Enter the folder path to merge .go files: ").strip()
output_file = input("Enter the output file path: ").strip()
if not os.path.isdir(folder_path):
print(f"The folder path {folder_path} does not exist or is not a directory.")
else:
merge_files_in_folder(folder_path, output_file)
print(f"All .go files in {folder_path} have been merged into {output_file}.")
@unstppbl
Copy link
Author

unstppbl commented Feb 3, 2025

Made to import the file into LLMs (chatgpt, gemini, etc)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment