{"@context":"https://w3id.org/codemeta/3.0","@type":"SoftwareSourceCode","identifier":"pkg:golang/github.com/jba/printsrc","name":"github.com/jba/printsrc","description":"Package printsrc prints Go values as Go source.\nIt strives to render legal Go source code, and returns\nan error when detects that it cannot.\n\nTo generate code for a slice of Points in package \"geo\":\n\nTo print the names of a type in another package, printsrc needs to know how to\nrefer to the package. Usually, but not always, the package identifier is the\nlast component of the package path. For example, the types in the standard\nlibrary package \"database/sql\" are normally prefixed by \"sql\". But this rule\ndoesn't hold for all packages. The actual identifier is the package name as\ndeclared in its files' package clause, which need not be the same as the last\ncomponent of the import path. (A common case: import paths ending in \"/v2\",\n\"/v3\" and so on.) Also, an import statement can specify a different identifier.\nSince printsrc can't know about these special cases, you must call\nPrinter.RegisterImport to tell it the identifier to use for a given import path.\n\nSometimes there is no way for printsrc to discover how to print a value as valid\nGo source code. For example, the math/big.Int type is a struct with no exported\nfields, so a big.Int cannot be printed as a struct literal. (A big.Int can be\nconstructed with the NewInt function or the SetString method.)\n\nUse Printer.RegisterPrinter to associate a type with a function that returns\nsource code for a value of that type.\n\nA custom printer for time.Time is registered by default. It prints a time.Time\nby printing a call to time.Date. An error is returned if the time's location is\nnot Local or UTC, since those are the only locations for which source\nexpressions can be produced.\n\nThis package makes an effort to sort map keys in order to generate deterministic\noutput. But if it can't sort the keys it prints the map anyway. The output\nwill be valid Go but the order of the keys will change from run to run.\nThat creates noise in code review diffs.\n\nUse Printer.RegisterLess to register a function that compares two values\nof a type. It will be called to sort map keys of that type.\n\nThis package elides the types of composite literals when it can.\nFor example, the value\n\nwill print in its simplified form,\n\nMaps with multiple NaN keys are not handled.\n\nThe reflect package provides no way to distinguish a type defined inside a\nfunction from one at top level. So printsrc will print expressions containing\nnames for those types which will not compile.\n\nSharing relationships are not preserved. For example, if two pointers in the input\npoint to the same value, they will point to different values in the output.\n\nUnexported fields of structs defined outside the generated package are ignored,\nbecause there is no way to set them (without using unsafe code). So important\nstate may fail to be printed. As a safety feature, printsrc fails if it is asked\nto print a non-zero struct from outside the generated package with no exported\nfields. You must register custom printers for such structs. But that won't catch\nproblems with types that have at least one exported field.\n\nCycles are detected by the crude heuristic of limiting recursion depth. Cycles\ncause printsrc to fail. A more sophisticated approach would represent cyclical\nvalues using intermediate variables, but it doesn't seem worth it.\n\n\nThis example shows how to use printsrc along with the text/template package\nto generate a file of Go code.","version":"v0.2.2","softwareVersion":"v0.2.2","license":"https://spdx.org/licenses/MIT","codeRepository":"https://github.com/jba/printsrc","issueTracker":"https://github.com/jba/printsrc/issues","url":"https://github.com/jba/printsrc","programmingLanguage":{"@type":"ComputerLanguage","name":"Go"},"dateCreated":"2021-12-12","dateModified":"2021-12-13","datePublished":"2021-12-13","copyrightYear":2021,"downloadUrl":"https://proxy.golang.org/github.com/jba/printsrc/@v/v0.2.2.zip","softwareHelp":{"@type":"WebSite","url":"https://pkg.go.dev/github.com/jba/printsrc#section-documentation"},"applicationCategory":"go","runtimePlatform":"go","developmentStatus":"active","sameAs":["https://pkg.go.dev/github.com/jba/printsrc"],"https://www.w3.org/ns/activitystreams#likes":12,"https://forgefed.org/ns#forks":1}