这个网站的教程不错,但是放到网站上可视化之后不好做笔记,而且要一页一页翻,不如搞一个打包的文档版。
预览:
先从这里:gobyexample-cn/gobyexample: Go by Example 通过例子学 Golang下载源代码
然后运行如下打包代码:
import os
# 根目录路径
base_path = "D:/A_projects/gobyexample-master/examples/"
# 目录顺序
sections = [
"hello-world", "values", "variables", "constants", "for", "if-else", "switch", "arrays", "slices", "maps",
"functions", "multiple-return-values", "variadic-functions", "closures", "recursion", "range", "pointers",
"strings-and-runes", "structs", "methods", "interfaces", "enums", "embedding", "generics", "range-over-channels",
"errors", "custom-errors", "goroutines", "channels", "channel-buffering", "channel-synchronization",
"channel-directions", "select", "timeouts", "non-blocking-channel-operations", "closing-channels",
"range-over-channels", "timers", "tickers", "worker-pools", "waitgroups", "rate-limiting", "atomic-counters",
"mutexes", "stateful-goroutines", "sorting", "sorting-by-functions", "panic", "defer", "recover",
"string-functions",
"string-formatting", "text-templates", "regular-expressions", "json", "xml", "time", "epoch",
"time-formatting-parsing", "random-numbers", "number-parsing", "url-parsing", "sha256-hashes", "base64-encoding",
"reading-files", "writing-files", "line-filters", "file-paths", "directories", "temporary-files-and-directories",
"embed-directive", "testing-and-benchmarking", "command-line-arguments", "command-line-flags",
"command-line-subcommands",
"environment-variables", "logging", "http-clients", "http-servers", "context", "spawning-processes",
"execing-processes", "signals", "exit"
]
# 生成Markdown内容
markdown_content = ""
index = 1 # 用于生成有序列表编号
for section in sections:
section_path = os.path.join(base_path, section)
if os.path.isdir(section_path):
# 加入小节标题和描述
markdown_content += f"{index}. {section.replace('-', ' ').title()}\n\n"
# 查找.go文件
go_file = os.path.join(section_path, f"{section}.go")
if os.path.isfile(go_file):
go_content = open(go_file, 'r', encoding="utf-8").read()
markdown_content += " ```go\n " + go_content.replace('\n', '\n ') + "\n ```\n\n"
# 查找.sh文件
sh_file = os.path.join(section_path, f"{section}.sh")
if os.path.isfile(sh_file):
sh_content = open(sh_file, 'r', encoding="utf-8").read()
markdown_content += " ```sh\n " + sh_content.replace('\n', '\n ') + "\n ```\n\n"
index += 1 # 更新列表编号
# 将内容写入Markdown文件
with open("gobyexample_tutorial.md", "w", encoding="utf-8") as f:
f.write(markdown_content)
print("Markdown文档已生成!")