{"id":13397142,"url":"https://github.com/sanity-io/litter","last_synced_at":"2025-05-11T11:13:40.832Z","repository":{"id":41811621,"uuid":"85597991","full_name":"sanity-io/litter","owner":"sanity-io","description":"Litter is a pretty printer library for Go data structures to aid in debugging and testing.","archived":false,"fork":false,"pushed_at":"2025-02-16T03:36:13.000Z","size":88,"stargazers_count":1583,"open_issues_count":19,"forks_count":59,"subscribers_count":25,"default_branch":"main","last_synced_at":"2025-05-11T11:13:36.938Z","etag":null,"topics":["go","golang","library","logging","mit-license","testing"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sanity-io.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2017-03-20T16:06:41.000Z","updated_at":"2025-05-10T02:31:02.000Z","dependencies_parsed_at":"2022-07-16T11:00:30.898Z","dependency_job_id":"ac7253ae-2d8a-47cb-b2b0-115e38dc1ee8","html_url":"https://github.com/sanity-io/litter","commit_stats":{"total_commits":83,"total_committers":20,"mean_commits":4.15,"dds":0.6265060240963856,"last_synced_commit":"f2ba02b77978d2fb5030483056f1939027fd1abc"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanity-io%2Flitter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanity-io%2Flitter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanity-io%2Flitter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanity-io%2Flitter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sanity-io","download_url":"https://codeload.github.com/sanity-io/litter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253554120,"owners_count":21926615,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","golang","library","logging","mit-license","testing"],"created_at":"2024-07-30T18:01:11.708Z","updated_at":"2025-05-11T11:13:40.786Z","avatar_url":"https://github.com/sanity-io.png","language":"Go","funding_links":[],"categories":["Go","开源类库","Open source library","Repositories","Programming Languages"],"sub_categories":["调试","Debugging","Golang"],"readme":"[![!Build Status](https://travis-ci.org/sanity-io/litter.svg?branch=master)](https://travis-ci.org/sanity-io/litter)\n\n# Litter\n\n**Litter is a pretty printer library for Go data structures to aid in debugging and testing.**\n\n---\n\nLitter is provided by\n\n\u003ca href=\"https://www.sanity.io/?utm_source=GitHub\u0026utm_campaign=litter\" rel=\"nofollow\" target=\"_blank\"\u003e\n\t\u003cimg src=\"https://www.sanity.io/static/images/logo_red.svg?v=2\" width=\"300\"\u003e\u003cbr /\u003e\n\tSanity: The Headless CMS Construction Kit\n\u003c/a\u003e\n\n---\n\nLitter named for the fact that it outputs *literals*, which you *litter* your output with. As a side benefit, all Litter output is syntactically correct Go. You can use Litter to emit data during debug, and it's also really nice for \"snapshot data\" in unit tests, since it produces consistent, sorted output. Litter was inspired by [Spew](https://github.com/davecgh/go-spew), but focuses on terseness and readability.\n\n### Basic example\n\nThis:\n\n```go\ntype Person struct {\n\tName   string\n\tAge    int\n\tParent *Person\n}\n\nlitter.Dump(Person{\n\tName:   \"Bob\",\n\tAge:    20,\n\tParent: \u0026Person{\n\t\tName: \"Jane\",\n\t\tAge:  50,\n\t},\n})\n```\n\nwill output:\n\n```\nPerson{\n\tName: \"Bob\",\n\tAge: 20,\n\tParent: \u0026Person{\n\t\tName: \"Jane\",\n\t\tAge: 50,\n\t},\n}\n```\n\n### Use in tests\n\nLitter is a great alternative to JSON or YAML for providing \"snapshots\" or example data. For example:\n\n```go\nfunc TestSearch(t *testing.T) {\n\tresult := DoSearch()\n\n\tactual := litterOpts.Sdump(result)\n\texpected, err := ioutil.ReadFile(\"testdata.txt\")\n\tif err != nil {\n\t\t// First run, write test data since it doesn't exist\n\t\tif !os.IsNotExist(err) {\n\t\t\tt.Error(err)\n\t\t}\n\t\tioutil.Write(\"testdata.txt\", actual, 0644)\n\t\tactual = expected\n\t}\n\tif expected != actual {\n\t\tt.Errorf(\"Expected %s, got %s\", expected, actual)\n\t}\n}\n```\n\nThe first run will use Litter to write the data to `testdata.txt`. On subsequent runs, the test will compare the data. Since Litter always provides a consistent view of a value, you can compare the strings directly.\n\n### Circular references\n\nLitter detects circular references or aliasing, and will replace additional references to the same object with aliases. For example:\n\n```go\ntype Circular struct {\n\tSelf *Circular\n}\n\nselfref := Circular{}\nselfref.Self = \u0026selfref\n\nlitter.Dump(selfref)\n```\n\nwill output:\n\n```\nCircular { // p0\n\tSelf: p0,\n}\n```\n\n## Installation\n\n```bash\n$ go get -u github.com/sanity-io/litter\n```\n\n## Quick start\n\nAdd this import line to the file you're working in:\n\n```go\nimport \"github.com/sanity-io/litter\"\n```\n\nTo dump a variable with full newlines, indentation, type, and aliasing information, use `Dump` or `Sdump`:\n\n```go\nlitter.Dump(myVar1)\nstr := litter.Sdump(myVar1)\n```\n\n### `litter.Dump(value, ...)`\n\nDumps the data structure to STDOUT.\n\n### `litter.Sdump(value, ...)`\n\nReturns the dump as a string\n\n## Configuration\n\nYou can configure litter globally by modifying the default `litter.Config`\n\n```go\n// Strip all package names from types\nlitter.Config.StripPackageNames = true\n\n// Hide private struct fields from dumped structs\nlitter.Config.HidePrivateFields = true\n\n// Hide fields matched with given regexp if it is not nil. It is set up to hide fields generate with protoc-gen-go\nlitter.Config.FieldExclusions = regexp.MustCompile(`^(XXX_.*)$`)\n\n// Sets a \"home\" package. The package name will be stripped from all its types\nlitter.Config.HomePackage = \"mypackage\"\n\n// Sets separator used when multiple arguments are passed to Dump() or Sdump().\nlitter.Config.Separator = \"\\n\"\n\n// Use compact output: strip newlines and other unnecessary whitespace\nlitter.Config.Compact = true\n\n// Prevents duplicate pointers from being replaced by placeholder variable names (except in necessary, in the case\n// of circular references)\nlitter.Config.DisablePointerReplacement = true\n```\n\n### `litter.Options`\n\nAllows you to configure a local configuration of litter to allow for proper compartmentalization of state at the expense of some comfort:\n\n``` go\n\tsq := litter.Options {\n\t\tHidePrivateFields: true,\n\t\tHomePackage: \"thispack\",\n\t\tSeparator: \" \",\n\t}\n\n\tsq.Dump(\"dumped\", \"with\", \"local\", \"settings\")\n```\n\n## Custom dumpers\n\nImplement the interface Dumper on your types to take control of how your type is dumped.\n\n``` go\ntype Dumper interface {\n\tLitterDump(w io.Writer)\n}\n```\n\nJust write your custom dump to the provided stream, using multiple lines divided by `\"\\n\"` if you need. Litter\nmight indent your output according to context, and optionally decorate your first line with a pointer comment\nwhere appropriate.\n\nA couple of examples from the test suite:\n\n``` go\ntype CustomMultiLineDumper struct {}\n\nfunc (cmld *CustomMultiLineDumper) LitterDump(w io.Writer) {\n\tw.Write([]byte(\"{\\n  multi\\n  line\\n}\"))\n}\n\ntype CustomSingleLineDumper int\n\nfunc (csld CustomSingleLineDumper) LitterDump(w io.Writer) {\n\tw.Write([]byte(\"\u003ccustom\u003e\"))\n}\n````\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsanity-io%2Flitter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsanity-io%2Flitter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsanity-io%2Flitter/lists"}