1
0
docreflect/readme.md

52 lines
1.8 KiB
Markdown
Raw Normal View History

2025-08-08 21:44:31 +02:00
# Docreflect
2026-01-21 22:09:19 +01:00
Library and tooling to help accessing go doc comments during runtime.
The docreflect library provides functions to access the Go doc based documentation of Go runtime objects, like
types, functions and methods, struct fields, or packages, during runtime, using their reflection handle to look
up the right snippet.
To be able to do that, the documentation needs to be captured during build time.
**Generating GoDoc registry code:**
2025-08-08 21:44:31 +02:00
2026-01-21 22:00:31 +01:00
Go doc comments are not accessible during runtime via reflection. To make them avaiable during runtime, we need
to capture them during build time. The docreflect command, or the docreflect/generate library package, fetches
the go doc comments of the specified declarations, and generates Go code that registers the docs for every
declaration. Code that includes the generated initialization code, will have the docs accessible via the top
level docreflect package methods.
2025-08-08 21:44:31 +02:00
2025-08-08 21:47:23 +02:00
**Declarations:**
2025-08-08 21:44:31 +02:00
- the declarations must be absolute Go paths
2026-01-21 22:00:31 +01:00
- when passing in the import path of a package, all the top level symbols of the package, plus the struct fields
and methods of the top level types will be included
2025-08-08 21:45:51 +02:00
- the package documentation can be fetched using `docreflect.Docs("absolute/import/path/of/package")`
2026-01-21 22:00:31 +01:00
- when passing in the import path of only the selected symbols, the rest of the package level symbols will be
ignroed
2025-08-08 21:44:31 +02:00
2025-08-17 18:33:25 +02:00
**Gotchas:**
2026-01-21 22:00:31 +01:00
2025-08-17 18:33:25 +02:00
- type aliases and type definitions based on named types are not resolved
2025-10-10 15:16:04 +02:00
- package imports are not resolved, necessary packages need to be included in the generate arguments
2025-08-17 18:33:25 +02:00
2025-08-08 21:44:31 +02:00
Library documentation: https://godocs.io/code.squareroundforest.org/arpio/docreflect
2026-01-21 22:09:19 +01:00
To install the docreflect command, run:
2025-08-08 21:44:31 +02:00
```
make install
```
Usage of the docreflect command:
```
2026-01-21 22:09:19 +01:00
docreflect \
mypackage \
coderepos.org/jdoe/mypackage \
coderepos.org/jdoe/otherpackage \
2026-01-21 22:00:31 +01:00
> docreflect.go
2025-08-08 21:44:31 +02:00
```
2025-10-10 15:12:11 +02:00
2026-01-21 22:00:31 +01:00
*Made in Berlin, DE*